在discord.py中发出say命令

2024-04-26 08:12:16 发布

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

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

提前谢谢


Tags: 代码py命令消息机器人somethingsaydiscord
2条回答

您可以在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”说出“msg”,bot应回复该参数:

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

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

        await ctx.message.delete()

最后,运行bot:

bot.run(token)

您还可以尝试以下更简单的方法:

@client.command()
async def say(ctx, message):
    if message != None
       ctx.channel.send(message)
    elif message == None:
       ctx.channel.send('Give me something to say')

相关问题 更多 >