【问题标题】:Making a say command in discord.py在 discord.py 中创建 say 命令
【发布时间】:2021-06-20 03:01:30
【问题描述】:

我想在 discord.py 中做一个简单的说命令(例如!说某事 - 机器人说“某事”并删除命令消息)但我发现的每个代码都不适合我。我是 python 和 discord.py 的新手,非常感谢一些帮助。

提前致谢。

【问题讨论】:

  • 欢迎来到 SO。 Please check 'how to ask a good question'。这是一个故障排除的地方;您展示您处理过的代码以及错误回溯,然后我们会帮助您了解正在发生的事情。

标签: python discord command discord.py bots


【解决方案1】:

你也可以试试这样更简单的方法:

@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')

【讨论】:

  • 比较None 通过is 而不是==!= 更符合Pythonic。这个代码块也没有完全回答这个问题,因为 OP 要求删除该命令。
【解决方案2】:

您可以在discord.py API reference 中找到很多有用的信息,了解您可以让您的不和谐机器人做什么。这是一个应该做你想做的事的例子:

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)

【讨论】:

  • 你真的不应该在这里灌输代码,这是一个应该帮助他人学习、获取知识的平台。如果你只是给别人代码,他们不会学到任何东西。
  • 但除此之外,这是一个非常好的答案!
猜你喜欢
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 2021-03-19
  • 2021-12-21
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
相关资源
最近更新 更多