【问题标题】:Discord.py, I can't receive message with @client.command()Discord.py,我无法使用@client.command() 接收消息
【发布时间】:2021-05-13 13:38:44
【问题描述】:

我无法接收@client.command() 的消息。 这看起来像我的代码:

import discord
from discord.ext import commands

client = commands.Bot(command_prefix='s!')

@client.command()
async def test(ctx):
    print(ctx.author)

client.run('')

谢谢

【问题讨论】:

  • 它正在将消息打印到控制台。不发送。使用 ctx.send 向 discord 发送消息

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


【解决方案1】:

使用ctx.channel.send() 将消息发送到调用命令的通道。

@client.command()
async def test(ctx):
    ctx.channel.send(ctx.author.name)

【讨论】:

    【解决方案2】:

    你要做的是作者的名字并打印出来。

    如果你想发送用户给的消息,你可以这样做:

    import discord
    from discord.ext import commands
    
    client =  commands.Bot(command_prefix = 's!')
    
    @client.event
    async def on_message(message):
      author = message.author
    
     # "!test" is the command of taking the message. Change with yours.
      if message.content.startswith('!test'):
        print(message.content)
        await test(author, message) #sends to a new method which shows message in channel.
    
    async def test(author, message):
        final_message = message.content[5:] #Slicing of the !test part. You also need to do it.
        await message.channel.send(final_message)
    
    client.run('')
    

    这里,

    • "!test" 是获取消息的命令。随你而变。

    • 分割命令部分以显示确切的消息。

    Example

    【讨论】:

    • 已编辑。谢谢。
    猜你喜欢
    • 2013-07-02
    • 1970-01-01
    • 2017-06-18
    • 2023-03-06
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多