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

2024-07-01 06:43:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始使用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'

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


Tags: inpyimporteventmessageonbotargs
1条回答
网友
1楼 · 发布于 2024-07-01 06:43:58

看起来您正在使用旧版本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

参考文献:

相关问题 更多 >

    热门问题