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

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

Having issues with my Discord bot not responding to prompts

问题

Code Base:

import os
import json
import time
import sys
import discord
import aiohttp
import random
import datetime
from aiohttp import ClientSession
from discord.ext import commands, tasks
from bot_framework import BotFramework

# 使用所需的命令前缀和意图创建 commands.Bot 的实例
bot = commands.Bot(command_prefix='~', intents=discord.Intents.default())

# 加载 cogs(功能模块)
for filename in os.listdir('./cogs'):
    # 检查文件是否是 Python 文件且不是隐藏文件
    if filename.endswith('.py') and not filename.startswith('__'):
        # 加载功能模块
        bot.load_extension('cogs.Bchat')
        bot.load_extension('cogs.Bgifs')
        bot.load_extension('cogs.Bmemes')

# 当机器人成功连接到 Discord 时的事件处理程序
@bot.event
async def on_ready():
    print(f'已登录为 {bot.user.name} - {bot.user.id}')
    # 设置机器人的状态
    await bot.change_presence(activity=discord.Game(name='~help'))

# 当命令执行失败时的事件处理程序
@bot.event
async def on_command_error(ctx, error):
    # 检查错误是否为 CommandNotFound 错误
    if isinstance(error, commands.CommandNotFound):
        # 如果是,发送消息通知用户未找到该命令
        await ctx.send('未找到命令。使用 ~help 查看可用命令。')
    else:
        # 如果是其他错误,将错误打印到控制台
        print(error)

# 使用提供的令牌运行机器人
bot.run('KEY')

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

英文:

Code Base:

import os
import json
import time
import sys
import discord
import aiohttp
import random
import datetime
from aiohttp import ClientSession
from discord.ext import commands, tasks
from bot_framework import BotFramework

# create an instance of commands.Bot with the desired command prefix and intents
bot = commands.Bot(command_prefix='~', intents=discord.Intents.default())

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

# event handler for when the bot has successfully connected to Discord
@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name} - {bot.user.id}')
    # set the bot's presence
    await bot.change_presence(activity=discord.Game(name='~help'))

# event handler for when a command fails to execute properly
@bot.event
async def on_command_error(ctx, error):
    # check if the error is a CommandNotFound error
    if isinstance(error, commands.CommandNotFound):
        # if so, send a message letting the user know the command was not found
        await ctx.send('Command not found. Use ~help to see available commands.')
    else:
        # if it's any other error, print the error to the console
        print(error)

# run the bot with the provided token
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

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

i see one big problem here:

# load the cogs
for filename in os.listdir('./cogs'):
    # check that the file is a Python file and not a hidden file
    if filename.endswith('.py') and not filename.startswith('__'):
        # load the cog
        bot.load_extension('cogs.Bchat')
        bot.load_extension('cogs.Bgifs')
        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

# load the cogs
for filename in os.listdir('cogs'):
    # check that the file is a Python file and not a hidden file
    if filename.endswith('.py') and not filename.startswith('__'):
        # load the cog
        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:

确定