cog文件内的所有内容似乎都是正确的,但却无法使用。

问题描述 投票:0回答:1

问题是,主文件和cog文件中的所有代码都是正确的,虽然由于某些原因导致 on_message() 不工作。在运行这个程序时,我没有收到任何异常,但在discord中没有任何作用。

这是主代码,也是加载齿轮的代码。

import discord
from discord.ext import commands
from discord.utils import get
import os
from dotenv import load_dotenv

load_dotenv('.env')
token = os.getenv('TOKEN')

bot = commands.Bot(command_prefix='$')
client = discord.Client()

@client.event
async def on_ready():
    print(' Made by Termed#6382')
    print(' Bot events are logged below : ')
    print(' ----------')
    for file in os.listdir('./cogs'):
        if file.endswith('.py') and not file.startswith('_'):
            bot.load_extension(f'cogs.{file[:-3]}')
            print(' Loaded {}'.format(file))

client.run(token)
bot.run(token)

这是cog文件,在一个cog文件夹里。

import discord
import os
from discord.ext import commands
from discord.utils import get
import asyncio

client = discord.Client()

class onMessage(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_message(message):
        message_content = message.content
        if message_content.isupper():
            if message.author.id == 'XX':
                await message.channel.send('{}No u'.format(message.author.mention))
            message_author = message.author
            await message.channel.send('{} , Please refrain from using too many capital letters.'.format(message.author.mention))
            print(' Warned {} for too many capital letters'.format(message_author))

def setup(bot):
    bot.add_cog(onMessage(bot))

python-3.x discord.py
1个回答
1
投票

你不需要同时使用这两个文件 bot = commands.Bot(command_prefix='$')client = discord.Client(). 这两个实例都会创建单独的Discord机器人实例,其中的 bot 使用 commands 延长。

移除 client 并使用 bot 贯穿整个代码。你也不需要在代码中指定 client = discord.Client() 在你的齿轮。

from discord.ext import commands
import os
from dotenv import load_dotenv

load_dotenv('.env')
token = os.getenv('TOKEN')

bot = commands.Bot(command_prefix='$')

@bot.event
async def on_ready():
    print(' Made by Termed#6382')
    print(' Bot events are logged below : ')
    print(' ----------')
    for file in os.listdir('./cogs'):
        if file.endswith('.py') and not file.startswith('_'):
            bot.load_extension(f'cogs.{file[:-3]}')
            print(' Loaded {}'.format(file))

bot.run(token)

齿轮

from discord.ext import commands

class onMessage(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_message(message):
        message_content = message.content
        if message_content.isupper():
            if message.author.id == 'XX':
                await message.channel.send('{}No u'.format(message.author.mention))
            message_author = message.author
            await message.channel.send('{} , Please refrain from using too many capital letters.'.format(message.author.mention))
            print(' Warned {} for too many capital letters'.format(message_author))

def setup(bot):
    bot.add_cog(onMessage(bot))
© www.soinside.com 2019 - 2024. All rights reserved.