在 discord.py 中创建 say 命令

时间:2021-03-23 12:49:40

标签: python discord command discord.py bots

我想在 discord.py 中创建一个简单的 say 命令(例如!say something - 机器人说“something”并删除命令消息)但是我找到的每个代码都对我不起作用。我是 python 和 discord.py 的新手,非常感谢您的帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以在 discord.py API reference 中找到许多关于可以让 Discord 机器人做什么的有用信息。这是一个应该按照您的意愿执行的示例:

import discord
from discord.ext import commands

设置您的机器人的意图和命令前缀以及您的机器人令牌:

intents = discord.Intents().default()
bot = commands.Bot(command_prefix='!', intents=intents)
token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'

使用参数“msg”定义命令“!say”,机器人应回复该命令:

@bot.command(name='say')
async def audit(ctx, msg=None):
    if msg is not None:
        await ctx.send(msg)

现在您可以删除调用命令的消息(需要权限!):

        await ctx.message.delete()

最后,运行机器人:

bot.run(token)