我正在制作一个带有欢迎消息的公共审核机器人,但是如何让人们设置欢迎频道? 我有这个,但是这行不通,它说它设置了频道,但是如果有人加入,则没有实际消息。有人可以帮我弄这个吗?这是我的代码:
async def on_member_join(member):
global welcome_channel_dict
channel_id = welcome_channel_dict[member.guild.id]
await client.get_channel(channel_id).send(f'{member.mention} welcome to the Otay! Support server! Enjoy your stay!?')
@client.command(name='welcome')
async def set_welcome_channel(ctx, channel: discord.TextChannel):
global welcome_channel_dict
welcome_channel_dict[ctx.guild.id] = channel.id
await ctx.send(f'Sent welcome channel for {ctx.message.guild.name} to {channel.name}')```
答案 0 :(得分:0)
您说过在开发人员门户中启用了意图,还记得在bot代码中定义意图吗?您需要设置:
import json
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = 'your_prefix', intents = intents)
Cloud为JSON提供了很好的教程。您可以在代码中执行类似的操作,以使bot重置时字典也不会重置。
在您的根目录中创建一个'guilds.json'文件。打开该字典,只需添加{}
,即可开始使用。
@client.event
async def on_member_join(member):
with open('guilds.json', 'r', encoding='utf-8') as f:
guilds_dict = json.load(f)
channel_id = guilds_dict[str(member.guild.id)]
await client.get_channel(int(channel_id)).send(f'{member.mention} welcome to the Otay! Support server! Enjoy your stay!?')
@client.command(name='welcome')
async def set_welcome_channel(ctx, channel: discord.TextChannel):
with open('guilds.json', 'r', encoding='utf-8') as f:
guilds_dict = json.load(f)
guilds_dict[str(ctx.guild.id)] = str(channel.id)
with open('guilds.json', 'w', encoding='utf-8') as f:
json.dump(guilds_dict, f, indent=4, ensure_ascii=False)
await ctx.send(f'Sent welcome channel for {ctx.message.guild.name} to {channel.name}')
# Optional:
# So if your bot leaves a guild, the guild is removed from the dict
@client.event
async def on_guild_remove(guild):
with open('guilds.json', 'r', encoding='utf-8') as f:
guilds_dict = json.load(f)
guilds_dict.pop(guild.id)
with open('guilds.json', 'w', encoding='utf-8') as f:
json.dump(guilds_dict, f, indent=4, ensure_ascii=False)