Python 3.x cog文件中的所有内容似乎都是正确的,但并不';行不通
问题是主文件和cog文件中的所有代码都是正确的,尽管出于某种原因,\u message()上的Python 3.x cog文件中的所有内容似乎都是正确的,但并不';行不通,python-3.x,discord.py,Python 3.x,Discord.py,问题是主文件和cog文件中的所有代码都是正确的,尽管出于某种原因,\u message()上的 不起作用。我在运行这个时没有得到任何异常,但它在不一致的情况下不会做任何事情 这是主代码,也会加载齿轮。 import discord from discord.ext import commands from discord.utils import get import os from dotenv import load_dotenv load_dotenv('.env') token = o
不起作用。我在运行这个时没有得到任何异常,但它在不一致的情况下不会做任何事情
这是主代码,也会加载齿轮。
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))
您不需要同时使用
bot=commands.bot(command_prefix='$')
和client=discord.client()
。它们都将使用命令
扩展创建单独的Discord bot实例
删除client
并在整个代码中使用bot
。您也不需要在cog中指定client=discord.client()
main
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)
cog
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))
您不需要同时使用
bot=commands.bot(command_prefix='$')
和client=discord.client()
。它们都将使用命令
扩展创建单独的Discord bot实例
删除client
并在整个代码中使用bot
。您也不需要在cog中指定client=discord.client()
main
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)
cog
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))