问题是尽管出于某种原因on_message()
,主文件和cog文件中的所有代码都是正确的
不起作用。运行此程序时,我没有收到任何异常,但是它并没有造成任何异议。
这是主要代码,它也会加载齿轮。
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))
答案 0 :(得分:2)
您无需同时使用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))