【问题标题】:How to send a message with a Discord.py如何使用 Discord.py 发送消息
【发布时间】:2021-03-11 13:04:13
【问题描述】:

我试图制作一个 Discord Bot,但我一直在努力让该 bot 向服务器发送消息。我编写了这段代码,它接收来自服务器的消息并将它们打印在终端中,如果有人可以通过重复机器人发送的消息的方式来回答这个问题,那就太好了。


import discord


client = discord.Client()

token = 'My token'

# bot is ready
@client.event
async def on_ready():
    try:
        
        print(client.user.name)
        print(client.user.id)
        print('Discord.py Version: {}'.format(discord.__version__))
    
    except Exception as e:
        print(e)


@client.event
async def on_message(message):
    # print message content in terminal
    print(message.content)


client.run(token)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:
    await message.channel.send("Bot's message")
    

    【讨论】:

    • 请添加一些关于如何回答问题的评论。具体来说,如果您解释了这行代码应该去哪里,这可能会有所帮助
    【解决方案2】:

    每当有人向文本频道发送“生日快乐”时,此代码都会发送一条消息。

    @client.event
    async def on_message(message):
        if message.author == client.user:
                return
        if message.content.lower() == "happy birthday":
                await message.channel.send("happy Birthday!")
        # print message content in terminal
        print(message.content)
    

    您可以以此为例。

    【讨论】:

    • @Odd'sAdventures 您可以通过将 if 语句替换为 if "happy birthday" in message.content.lower() 来做到这一点
    • @Odd'sAdventures 这将检查消息是否在内容中,而不是检查它是在开头还是结尾。
    猜你喜欢
    • 2021-02-24
    • 2021-07-08
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2020-11-10
    • 2020-10-31
    • 2017-08-15
    • 2022-08-13
    相关资源
    最近更新 更多