【发布时间】:2021-11-30 09:13:26
【问题描述】:
我最近开始学习 discord.py,我想为多频道添加欢迎消息,我发现这篇文章 Discord.py welcome message for multiple servers 我尝试了代码,似乎可以正常工作,但我遇到了问题使用此代码创建欢迎消息,因为我没有使用 discord.py 的经验 谢谢
@client.event
async def on_guild_join(guild):
#loads json file to dictionary
with open("welcome-message.json", "r") as f:
guildInfo = json.load(f)
guildInfo[guild.id] = guild.text_channels[0] #sets key to guilds id and value to top textchannel
#writes dictionary to json file
with open("welcome-message.json", "w") as f:
json.dump(guildInfo, f)
#allows server members to set channel for welcome messages to send to
@client.command()
async def welcomeMessage(ctx):
with open("welcome-message.json", "r") as f:
guildInfo = json.load(f)
guildInfo[ctx.message.guild.id] = ctx.message.channel.id #sets channel to send message to as the channel the command was sent to
with open("welcome-message.json", "w") as f:
json.dump(guildInfo, f)
# welcome code
@client.event
async def on_member_join(ctx, message):
with open("welcome-message.json", "r")as f:
guildInfo = json.load(f)
channel = guildInfo[ctx.message.guild.id]
embed = discord.Embed(title="Welcome ""!",
description=f"We're so glad you're here !", color=discord.Color.green())
await channel.send(embed=embed) ```
【问题讨论】:
-
这有什么问题?
标签: discord.py