遇到 Discord 机器人不响应提示的问题。

huangapple go评论158阅读模式
英文:

Having issues with my Discord bot not responding to prompts

问题

Code Base:

  1. import os
  2. import json
  3. import time
  4. import sys
  5. import discord
  6. import aiohttp
  7. import random
  8. import datetime
  9. from aiohttp import ClientSession
  10. from discord.ext import commands, tasks
  11. from bot_framework import BotFramework
  12. # 使用所需的命令前缀和意图创建 commands.Bot 的实例
  13. bot = commands.Bot(command_prefix='~', intents=discord.Intents.default())
  14. # 加载 cogs(功能模块)
  15. for filename in os.listdir('./cogs'):
  16. # 检查文件是否是 Python 文件且不是隐藏文件
  17. if filename.endswith('.py') and not filename.startswith('__'):
  18. # 加载功能模块
  19. bot.load_extension('cogs.Bchat')
  20. bot.load_extension('cogs.Bgifs')
  21. bot.load_extension('cogs.Bmemes')
  22. # 当机器人成功连接到 Discord 时的事件处理程序
  23. @bot.event
  24. async def on_ready():
  25. print(f'已登录为 {bot.user.name} - {bot.user.id}')
  26. # 设置机器人的状态
  27. await bot.change_presence(activity=discord.Game(name='~help'))
  28. # 当命令执行失败时的事件处理程序
  29. @bot.event
  30. async def on_command_error(ctx, error):
  31. # 检查错误是否为 CommandNotFound 错误
  32. if isinstance(error, commands.CommandNotFound):
  33. # 如果是,发送消息通知用户未找到该命令
  34. await ctx.send('未找到命令。使用 ~help 查看可用命令。')
  35. else:
  36. # 如果是其他错误,将错误打印到控制台
  37. print(error)
  38. # 使用提供的令牌运行机器人
  39. bot.run('KEY')

这是您提供的代码的翻译。如果需要更多帮助,请告诉我。

英文:

Code Base:

  1. import os
  2. import json
  3. import time
  4. import sys
  5. import discord
  6. import aiohttp
  7. import random
  8. import datetime
  9. from aiohttp import ClientSession
  10. from discord.ext import commands, tasks
  11. from bot_framework import BotFramework
  12. # create an instance of commands.Bot with the desired command prefix and intents
  13. bot = commands.Bot(command_prefix='~', intents=discord.Intents.default())
  14. # load the cogs
  15. for filename in os.listdir('./cogs'):
  16. # check that the file is a Python file and not a hidden file
  17. if filename.endswith('.py') and not filename.startswith('__'):
  18. # load the cog
  19. bot.load_extension('cogs.Bchat')
  20. bot.load_extension('cogs.Bgifs')
  21. bot.load_extension('cogs.Bmemes')
  22. # event handler for when the bot has successfully connected to Discord
  23. @bot.event
  24. async def on_ready():
  25. print(f'Logged in as {bot.user.name} - {bot.user.id}')
  26. # set the bot's presence
  27. await bot.change_presence(activity=discord.Game(name='~help'))
  28. # event handler for when a command fails to execute properly
  29. @bot.event
  30. async def on_command_error(ctx, error):
  31. # check if the error is a CommandNotFound error
  32. if isinstance(error, commands.CommandNotFound):
  33. # if so, send a message letting the user know the command was not found
  34. await ctx.send('Command not found. Use ~help to see available commands.')
  35. else:
  36. # if it's any other error, print the error to the console
  37. print(error)
  38. # run the bot with the provided token
  39. bot.run('KEY')

I was using this code as a base for my Discord bot to load in some supplemental cogs for things like GIFS , basic chat stuff etc.

I tried implementing intents , ( seen on line 14 ) but now it isnt responding to tilde ~ . Not sure why, any help would be appreciated!

Tried reverting back to old version of bot

Tried adjusting intents declaration

答案1

得分: 1

  1. # 加载模块
  2. for filename in os.listdir('cogs'):
  3. # 检查文件是否是 Python 文件且非隐藏文件
  4. if filename.endswith('.py') and not filename.startswith('__'):
  5. # 加载模块
  6. bot.load_extension(f'cogs.{filename[:-3]}')
英文:

i see one big problem here:

  1. # load the cogs
  2. for filename in os.listdir('./cogs'):
  3. # check that the file is a Python file and not a hidden file
  4. if filename.endswith('.py') and not filename.startswith('__'):
  5. # load the cog
  6. bot.load_extension('cogs.Bchat')
  7. bot.load_extension('cogs.Bgifs')
  8. bot.load_extension('cogs.Bmemes')

what is this for a random check and loop? this will load some static cogs for every file that ends with py in a directory?
doesnt make really sense.

if you want to load all cogs in the directory if they dont start with __
make soemthing like

  1. # load the cogs
  2. for filename in os.listdir('cogs'):
  3. # check that the file is a Python file and not a hidden file
  4. if filename.endswith('.py') and not filename.startswith('__'):
  5. # load the cog
  6. bot.load_extension(f'cogs.{filename[:-3]}')

or just load the cogs static

huangapple
  • 本文由 发表于 2023年5月6日 21:53:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定