如何让discord py bot在被邀请到服务器时发送消息

how to make discord py bot send a message when it is invited to a server

我有一个 discord.py 机器人,我使用这个脚本让机器人在被邀请到服务器时向主频道发送消息! 但是,这不会向服务器发送任何内容!请帮忙!

@client.event
async def on_guild_join(guild):
    print("NEW SERVER WAS JOINED!")
    general = find(lambda x: x.name == 'general',  guild.text_channels)
    if general and general.permissions_for(guild.me).send_messages:  
        await general.send(f"""
████████╗░░░░░░██████╗░░█████╗░████████╗
╚══██╔══╝░░░░░░██╔══██╗██╔══██╗╚══██╔══╝
░░░██║░░░█████╗██████╦╝██║░░██║░░░██║░░░
░░░██║░░░╚════╝██╔══██╗██║░░██║░░░██║░░░
░░░██║░░░░░░░░░██████╦╝╚█████╔╝░░░██║░░░
░░░╚═╝░░░░░░░░░╚═════╝░░╚════╝░░░░╚═╝░░░
```Thanks For Inviting the bot!
Made with <3 by TOG6#6666 with Tons of help from the TOG6 community!```
***Server Invite:*** https://discord.gg/vSxuAbq""")
        await general.send(f"``` Type $help to view all commands!```")

我想感谢大家回复这个post!但是我想出了一个逻辑!

由于我的机器人需要管理员权限,它可以 post 在任何频道上!

现在的情况是,每当有人说 T-BOT(区分大小写)时,机器人 post 就是主要消息

那样的话,没有人会同时完整地说出机器人,机器人 post 是它的第一条消息!

对于那些想知道如何做的人,您可以使用 on_message discord 事件让它搜索机器人名称!

我希望我能帮助将来制作 discord.py 机器人的人! :-)

我上次回答说你可以使用 on_message,但后来我想出了一个好 2,000 倍的东西!每个服务器都有一个系统频道,使用您的 discord.py 机器人,您可以找到系统频道并让它向那里发送消息!

这是最好的方法,因为大多数服务器所有者都将主要或重要频道作为他们的系统频道!

@client.event
async def on_guild_join(guild):
    for channel in guild.text_channels:
        if channel.permissions_for(guild.me).send_messages:
            await channel.send('I will send this message when I join a server')
        break

我更擅长 discord.js,但我会解决你的 discord.py 问题......

@client.event
async def on_guild_join(guild):
    try:
        joinchannel = guild.system_channel
        # The system channel is where Discord’s join messages are sent
        await joinchannel.send('Thanks for inviting me to your server!')
    catch:
        # if no system channel is found send to the first channel in guild
        await guild.text_channels[0].send(<message>)