Discord Bot 加入消息

时间:2021-06-14 06:49:26

标签: python discord

所以我对 python 和 discord bots 有一些经验,但我似乎无法弄清楚如何做到这一点,以便当机器人第一次加入服务器时,它会发送一条消息,如“嗨,使用 !help for帮助”或类似的东西。谢谢。

2 个答案:

答案 0 :(得分:0)

您想在哪个文本频道中发送消息?

您可以使用TextChannel.send在公会中发送消息,并定义方法on_guild_join,当机器人加入服务器时将调用该方法。下面是一个示例,机器人在服务器的 system_channel 中发送消息(如果存在):

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!")

@bot.event
async def on_guild_join(guild):
    if guild.system_channel:
        await guild.system_channel.send("Hi, use !help for help")

以下是机器人将在服务器中的随机文本通道中发送消息的示例:

import discord
from discord.ext import commands
from random import choice

bot = commands.Bot(command_prefix="!")

@bot.event
async def on_guild_join(guild):
    if guild.text_channels: # If the guild has at least one text channel
        channel = choice(guild.text_channels)
        await channel.send("Hi, use !help for help")

答案 1 :(得分:0)

谢谢。当我尝试这个时,我完全忘记了公会部分。