如何设置欢迎频道discord.py? [英] How to set a welcome channel discord.py?
本文介绍了如何设置欢迎频道discord.py?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一个带有欢迎信息的公共审核机器人,但我如何才能让人们设置他们的欢迎频道? 我有这个,但这个不起作用,它说它设置了频道,但如果有人加入,就没有实际的消息。有没有人能帮我一下?这是我的代码:
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}')```
推荐答案
您说您在开发人员门户中启用了意图,您记得在bot代码中定义意图吗?您需要设置:
import json
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = 'your_prefix', intents = intents)
Cloud为JSON提供了一个非常好的教程。您可以在代码中执行类似的操作,这样每当您的机器人重置时,DICT就不会重置。
在您的根目录中创建一个‘guids.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)
这篇关于如何设置欢迎频道discord.py?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文