【问题标题】:welcome message code for multiple servers多个服务器的欢迎消息代码
【发布时间】: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


【解决方案1】:

您必须指定member intent 才能接收on_member_join 事件。您可以在创建机器人对象时通过添加意图选项来执行此操作:

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(intents=intents, other_options)

注意

如果您的机器人经过验证,您需要SERVER MEMBERS INTENT,您可以在应用程序的机器人页面上请求。

还有:

在这一行: embed = discord.Embed(title="Welcome ""!", 它会出错,因为带引号的字符串中有引号,如果要保留这些引号,请将引号切换为单引号 (')

【讨论】:

  • 您好,谢谢您的回复,所以我添加了前缀intents=intents,现在看起来像这样client = commands.Bot(command_prefix=get_prefix, intents=intents,),但是当我尝试使用我的测试帐户加入频道时,我收到错误@987654329 @witch 是欢迎代码中的`channel = guildInfo[ctx.message.guild.id]`
  • @TK12481 这是因为on_member_join 中传递的参数是成员对象,如in the docs 所示。所以要获得公会ID,它将是ctx.guild.id
  • 非常感谢您的帮助,现在一切正常 :)
  • 很高兴知道,祝你项目的其余部分好运:)
猜你喜欢
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 2018-09-20
  • 2018-10-06
  • 2021-12-24
相关资源
最近更新 更多