Python 如何让你的Discord机器人说一些具体的话,然后删除之前的消息

Python 如何让你的Discord机器人说一些具体的话,然后删除之前的消息,python,discord,discord.py,discord.py-rewrite,Python,Discord,Discord.py,Discord.py Rewrite,我刚开始使用discord.py,基本上我只是想让我的discord机器人说点什么,然后删除之前的文本,例如,我想键入“/说你好”,然后我想机器人抓取它,删除前缀,然后只打印“你好”,我已经在谷歌上搜索并找到了另一个指南,但没有后续的答案,当我尝试他们出错的解决方案时,下面是我使用的代码 import discord from discord.ext import commands bot = discord.Client() prefix = "/" @bot.event asyn

我刚开始使用discord.py,基本上我只是想让我的discord机器人说点什么,然后删除之前的文本,例如,我想键入“/说你好”,然后我想机器人抓取它,删除前缀,然后只打印“你好”,我已经在谷歌上搜索并找到了另一个指南,但没有后续的答案,当我尝试他们出错的解决方案时,下面是我使用的代码

    import discord
from discord.ext import commands

bot = discord.Client()
prefix = "/"

@bot.event
async def on_ready():
    print("Online")

@bot.event
async def on_message(message):
    args = message.content.split(" ")[1:]
    if message.content.startswith(prefix + "say"):
        await bot.delete_message(message)
        await bot.send_message(message.channel, " ".join(args))

bot.run("token")
这是控制台打印出来的错误

C:\Users\unknownuser\anaconda3\envs\discordbot\pythonw.exe C:/Users/unknownuser/PycharmProjects/discordbot/bot.py
Online
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\unknownuser\anaconda3\envs\discordbot\lib\site-packages\discord\client.py", line 313, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/unknownuser/PycharmProjects/discordbot/bot.py", line 15, in on_message
    await bot.delete_message(message)
AttributeError: 'Client' object has no attribute 'delete_message'


当我开始学习它背后的文档和逻辑时,我应该开始自己弄清楚它,但这一点让我感到困惑,希望您能提供帮助。

看起来您使用的是旧版本discord.py的教程。 在最新的重写版本中有一些

代码示例
#使用命令decorator
@bot.command()
异步定义语句(ctx,*,句子):
等待ctx.message.delete()
等待ctx发送(句子)
#############################################
#使用on_消息事件
@机器人事件
异步def on_消息(消息):
args=message.content.split(“”[1:]
如果message.content.startswith(前缀+“say”):
等待消息。删除()
等待消息.channel.send(“.join(args))
其他:
等待bot.process_命令(消息)#允许修饰命令工作

参考文献:


谢谢,在阅读了文档之后,我终于让它工作起来了,这是你试图解决的问题之一,放弃吧。然后,在等待别人帮助你的时候,你就明白了。另外,您可以解释一下使用message event和command Detector的区别吗?使用decorators时,组织代码要容易得多,尤其是对于大型机器人。它使您能够将代码拆分为“”(只是不同的模块),并且看起来更整洁。处理争论也要容易得多。