【问题标题】:How do i put more than one command leading to the same response in discord.py?如何在 discord.py 中放置多个导致相同响应的命令?
【发布时间】:2021-01-29 11:31:50
【问题描述】:
@client.event
async def on_message(message):
    if message.content == "fase":
        channel = (mychannel)
        await message.channel.send("Fase 1")

我正在尝试让 message.content 检测多个单词并发送相同的 message.channel.send 我试过了

if message.content.lower() == "fase", "estagio", "missao", "sala":
if message.content.lower() == ("fase", "estagio", "missao", "sala"):
if message.content.lower() == "fase" or  "estagio" or "missao" or "sala":
if message.content.lower() == ("fase" or  "estagio" or "missao" or "sala"):

我读了这篇文章:How do I allow for multiple possible responses in a discord.py command?

这是完全相同的问题,但在他的情况下,这是我已经在我的代码中修复的 CaseSensitiveProblem

多个单词的第二个代码是:

bot = commands.Bot(command_prefix='!', case_insensitive=True)
@bot.command(aliases=['info', 'stats', 'status'])
    async def about(self):
        # your code here

我做到了,但出现了很多错误,导致机器人甚至无法运行(我将 PyCharm 与 discord.py 1.4.1 和 python 3.6 一起使用):

#import and token things up here
bot = commands.Bot(command_prefix='i.')
@bot.command(aliases=['fase', 'estagio', 'missao', 'sala']) #'@' or 'def' expected
    async def flame(self): #Unexpected indent // Unresolved reference 'self'
        if message.content(self): #Unresolved reference 'message'
            await message.send("Fase 1") #Unresolved reference 'message' // Statement expected, found Py:DEDENT


我能做些什么来解决它?

【问题讨论】:

  • if message.content.lower() in ("fase", "estagio", "missao", "sala")
  • 我忘记了“in”的存在,谢谢你帮了我很多。

标签: python discord.py


【解决方案1】:

下面是Commands扩展的使用方法:

from discord.ext import commands

bot = commands.Bot(command_prefix='!', case_insensitive=True)
@bot.command(aliases=['info', 'stats', 'status'])
async def about(ctx):
    #Your code here

每个命令都有以下共同点:

  • 它们是使用 bot.command() 装饰器创建的。
  • 默认情况下,命令名是函数名。
  • 装饰器和函数定义必须具有相同的缩进级别。
  • ctx(第一个参数)将是一个 discord.Context 对象,其中包含很多信息(消息作者、频道和内容、discord 服务器、使用的命令、调用命令的别名……)

那么,ctx 允许你使用一些快捷键:

  • message.channel.send() 变为 ctx.send()
  • message.author 变为 ctx.author
  • message.channel 变为 ctx.channel

命令参数也更容易使用:

from discord import Member
from discord.ext import commands

bot = commands.Bot(command_prefix='!', case_insensitive=True)

#Command call example: !hello @Mr_Spaar
#Discord.py will transform the mention to a discord.Member object
@bot.command()
async def hello(ctx, member: Member):
    await ctx.send(f'{ctx.author.mention} says hello to {member.mention}')

#Command call example: !announce A new version of my bot is available!
#"content" will contain everything after !announce (as a single string)
@bot.command()
async def announce(ctx, *, content):
    await ctx.send(f'Announce from {ctx.author.mention}: \n{content}')

#Command call example: !sum 1 2 3 4 5 6
#"numbers" will contain everything after !sum (as a list of strings)
@bot.command()
async def sum(ctx, *numbers):
    numbers = [int(x) for x in numbers]
    await ctx.send(f'Result: {sum(numbers)}')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2021-02-13
    • 2019-10-23
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多