如何使不和谐的py bot在被邀请到服务器时发送消息

时间:2020-07-28 05:44:31

标签: python python-3.x discord.py discord.py-rewrite

我有一个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!```")

4 个答案:

答案 0 :(得分:1)

我要感谢大家对这篇文章的答复!但是我想出了一个逻辑!

由于我的机器人具有管理员权限,因此它可以在任何频道上发布!

现在的事情是,只要有人说出T-BOT(区分大小写),该机器人就会发布主要消息

那样,没有人会同时说出该机器人的全部内容,该机器人会发布其第一条消息!

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

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

答案 1 :(得分:1)

@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

答案 2 :(得分:1)

我更擅长 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>)

答案 3 :(得分:0)

我上次回答说可以使用on_message,但是后来我想到了2,000倍更好的方法!每个服务器都有一个系统通道,并且使用discord.py bot,您可以找到系统通道并将其发送到那里!

这是最好的方法,因为大多数服务器所有者将主渠道或重要渠道保留为系统渠道!