【问题标题】:multiple welcome messages on multiple servers discord.py多个服务器上的多个欢迎消息 discord.py
【发布时间】:2021-05-10 02:34:17
【问题描述】:

我是这个平台的新手,也是 python 的新手。我想创建一个欢迎活动,向新用户问好。 (以德语显示的信息) 它工作正常,但是如果我复制事件并只是放入其他 ID,它就无法正常工作。我想拥有多个服务器,让我的机器人向新成员打招呼,还有多个频道。我不知道我怎样才能让它在不止一台服务器上打招呼。我也不确切知道如何读取文件。没有文件的方法会很棒。但我也可以处理文件,因为如果我知道如何处理文件,我可以尝试另一个命令。这是我有 2 台服务器的代码。我想你可以看到我已经尝试过。但是如果我加入主服务器,它会在主服务器中发送 2 条消息,如果我加入测试服务器,它会在主服务器中发送 1 条消息。因此,它不会在测试服务器中发送任何消息。有人有想法吗?

import discord
from discord.ext import commands
import Cogs
import main
bot = commands.Bot(command_prefix=test_prefix, case insensitive=True, intents=intents

    @bot.event
    async def on_member_join(member):
        #testserver
        guild = bot.get_guild(836268774465208380)
        channel = guild.get_channel(836659340391874600)
        embed = discord.Embed(title=f'Herzlich Willkommen auf {guild.name}!', description=f'Heißen wir {member.mention} '
                                                                                          f'herzlich willkommen! Hab viel '
                                                                                          f'Spaß auf unserem Server!'
                                                                                          f' :smile:',
                              colour=discord.Colour.from_rgb(146, 4, 30))
        embed.set_footer(text=f'Bot by {botowner}', icon_url=f'{avatarowner}')
        embed.set_thumbnail(url=guild.icon_url)
        embed.set_author(name=f'{member.name}', icon_url=f'{member.avatar_url}')
        await channel.send(embed=embed)
    
    @bot.event
    async def on_member_join(member):
        #my main server
        guild = bot.get_guild(572545560758976514)
        channel = guild.get_channel(572545561371213826)
        embed2 = discord.Embed(title=f'Herzlich Willkommen auf {guild.name}!', description=f'Heißen wir {member.mention} '
                                                                                          f'herzlich willkommen! Hab viel '
                                                                                          f'Spaß auf unserem Server!'
                                                                                          f' :smile:',
                              colour=discord.Colour.from_rgb(146, 4, 30))
        embed2.set_footer(text=f'Bot by {botowner}', icon_url=f'{avatarowner}')
        embed2.set_thumbnail(url=guild.icon_url)
        embed2.set_author(name=f'{member.name}', icon_url=f'{member.avatar_url}')
        await channel.send(embed=embed2)

bot.run(test_token)

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    首先,您在声明bot 的那一行缺少),而这两个on_member_join 都应该是有意的,我认为这可能是复制时出错。 其次,您只能拥有每个事件一次,因此您必须将它们合并为一个。 第三,关于你的问题。如果您打算在某个时候向公众展示您的机器人,则必须使用一个文件,这样您就可以通过命令更改每个公会中的欢迎频道,而不必在每次机器人重新启动时都重做。要读取 json 文件,请使用

    with open("your_json_file.json", "r") as json_file:
        json_dict = json.load(json_file) #this gets the contents of the json file and loads them to a dict object.
    

    并写入 json 文件:

    with open("your_json_file.json", "w") as json_file:
        json.dump(json_dict, json_file) #here you change to contents of json_file to json_dict
    

    如果您希望公会所有者能够更改他们的欢迎频道,您必须执行一个命令来更改它,该命令将首先读取新的欢迎频道,然后将其写入 json 文件。在这段代码中,我将使用静态字典,每次机器人重新启动时都会重置。您还必须阅读 on_member_join 事件顶部的 json 文件。 现在看代码: 您在代码顶部声明welcome_channels,如下所示:

    welcome_channels = {"First Guild id here":"First guild welcome channel id here", "Second Guild id here":"Second guild welcome channel id here", etc}
    

    那么,既然您只能使用一个on_member_join 事件,请这样做:

    @bot.event
    async def on_member_join(member):
        #maybe read json file here
        if member.guild.id in list(welcome_channels.keys):
            guild = member.guild
            channel = guild.get_channel(welcome_channels[member.guild.id])
            #The rest of your code can stay the same, except remove the second on_member_join
    

    参考文献

    【讨论】:

    • 谢谢!我想我对于 json 文件来说太笨了,所以我使用了带有welcome_channels 的方法以及来自频道和行会的 id。我只想拥有 5 台服务器,带有欢迎和再见,因为再见与欢迎相同,所以我也可以在那里使用它。谢谢你的帮助!
    猜你喜欢
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2021-10-05
    相关资源
    最近更新 更多