【问题标题】:Everything inside a cog file seems to be correct, but doesn't workcog 文件中的所有内容似乎都是正确的,但不起作用
【发布时间】:2020-06-03 07:42:48
【问题描述】:

问题是主文件和 cog 文件中的所有代码都是正确的,尽管出于某种原因on_message() 不起作用。运行它时我没有遇到任何异常,但它没有做任何不和谐的事情。

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

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】:

    您不需要同时使用bot = commands.Bot(command_prefix='$')client = discord.Client()。它们都将创建单独的 Discord 机器人实例,bot 使用 commands 扩展名。

    删除 client 并在整个代码中使用 bot。您也无需在 cog 中指定 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))
    

    【讨论】:

      猜你喜欢
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      相关资源
      最近更新 更多