我的 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')
答案 0 :(得分:0)
对于 on_member_join()
事件参考,您需要打开 Server Members Intent 特权网关 Intent。这必须在您的机器人页面以及您的脚本(您已经完成)中完成:
在下图中,它将是特权网关意图的第二个。
您的代码有问题可能是因为您在 cog 文件和主文件中都定义了一个 discord.Bot
实例。由于 setup(client)
在您的 cog 文件中定义并且 client.load_extension()
在您的主文件中被调用,您应该从您的 cog 文件中删除以下几行:
cog.py
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
但是,为了保存 Intent,您需要在 discord.Bot()
调用之前添加以下几行:
main.py
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=".", intents=intents)
对于您的代码,我建议使用 Client.fetch_channel
method,因为它消除了不正确的公会 ID 导致 channel
为 None
的可能性。
async def on_member_join(self, member):
channel = await client.fetch_channel(1234567890)
if channel is not None:
await channel.send(f"Welcome {member.mention}.")
或者,您可以只使用 discord.utils.get()
和 member.guild.channels
:
async def on_member_join(self, member):
channel = discord.utils.get(member.guild.channels, id=1234567890)
if channel is not None:
await channel.send(f"Welcome {member.mention}.")
只是减少潜在错误的建议。
答案 1 :(得分:0)
如何修复:
主要文件
import discord
import os
from discord.ext import commands
intents = discord.Intents.default()
client = commands.Bot(command_prefix = '.', intents=intents)
intents.members = True
@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')
COG 文件
import discord
from discord.ext import commands
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):
print(member)
await member.send("hello")
guild = self.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")
@commands.command()
async def Modulo_benvenuto(self, ctx):
await ctx.send('test')
def setup(client):
client.add_cog(Welcome(client))