如何让Discord机器人说出具体的话,然后删除上一条消息

时间:2020-05-14 12:05:03

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

我是第一次使用discord.py,基本上,我只是想让我的discord机器人说些话然后删除之前的文本,例如,我想输入“ / say hello”,然后我希望该机器人抓取它,删除前缀并仅打印“ hello”,Ive已经在Google上搜索并找到了另一本指南,但是没有后续答案,当我尝试错误的解决方案时,下面是使用im的代码

    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'

在我开始学习其背后的文档和逻辑时,我应该开始自己弄清楚它,但是这让我很困惑,帮助将不胜枚举

1 个答案:

答案 0 :(得分:1)

好像您正在使用旧版本discord.py的教程。 最新的[重写]版本中有一些major changes

您的代码示例

# using the command decorator
@bot.command()
async def say(ctx, *, sentence):
    await ctx.message.delete()
    await ctx.send(sentence)

#############################################

# using the on_message event
@bot.event
async def on_message(message):
    args = message.content.split(" ")[1:]
    if message.content.startswith(prefix + "say"):
        await message.delete()
        await message.channel.send(" ".join(args))
    else:
        await bot.process_commands(message) # allows decorated commands to work

参考: