【发布时间】:2021-04-14 23:56:04
【问题描述】:
我的on_member_join 监听器没有正确执行。这个想法是,当一个新人进入我的 Discord 时,我的机器人会用自定义消息向他们打招呼。
没有语法错误并且类加载正确。 on_ready 侦听器正确响应 Welcome: ON。如果我尝试进行调试打印,则不会执行。
我哪里做错了?我不明白,这对我来说似乎是正确的。
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
class Welcome(commands.Cog):
def __init__(self,client):
self.client=client
@commands.Cog.listener()
async def on_ready(self):
print("Welcome: ON")
@commands.Cog.listener()
async def on_member_join(self, member):
guild= client.get_guild(828676048039706694)
channel=guild.get_channel(828676048039706697)
if channel is not None:
await channel.send(f'Welcome {member.mention}.')
@commands.command()
async def Modulo_benvenuto(self,ctx):
await ctx.send('')
def setup(client):
client.add_cog(Welcome(client))
这是我的主文件:
import discord
import os
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
@client.command()
async def load(ctx, extension):
client.load_extension(f'cogs.{extension}')
@client.command()
async def unload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
@client.command()
async def reload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
client.load_extension(f'cogs.{extension}')
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
client.run('TOKEN')
有了一个新的机器人就可以了,代码如下:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print("Bot is on")
@client.event
async def on_member_join(member):
print(member)
await member.send("hello")
guild = client.get_guild(831588406089744435)
channel = discord.utils.get(member.guild.channels, id=831588406089744438)
if guild:
print("guild ok")
else:
print("guild not found")
if channel is not None:
await channel.send(f'Welcome to the {guild.name} Discord Server, {member.mention} ! :partying_face:')
else:
print("id channel wrong")
client.run('TOKEN')
【问题讨论】:
-
你试过合并两个文件/代码吗?
-
不行,明天我试试
-
没有给我这个错误:
TypeError: event() missing 1 required positional argument: 'coro'
标签: python discord.py