discord.py 8ball 命令但没有 bot 前缀

discord.py 8ball command but without bot prefix

我正在为一个朋友编写一个机器人,他们要我制作一个 8ball 命令。你认为这看起来很容易.. 但他们不希望前缀包含命令。所以它会是这样的: BotName, do you think today will be a good day?

我试过使用 @client.event,但我不知道如何让用户可以说出他们自己的问题,但在问题的前面有机器人名称。 所以像这样: BotName, do you think today will be a good day?

需要包含 BotName 部分才能触发事件。然后他们可以说出他们想说的话。像这样(上面已经给出了例子):

BotName, do you think today will be a good day?

这是我试过的一些代码:

import discord
from discord.ext import commands
import random

class eightball(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_message(self,message):
        #botname = [f"BotName, {message}?"] #tried this too
       # if message.content in botname: #tried this too
        if f'BotName, {message.author.content}?' in message.content:
            responses = ['Responses here.']
            await message.channel.send(f'{random.choice(responses)}')
            await self.client.process_commands(message)

def setup(client):
    client.add_cog(eightball(client))

如果不可能,请不要担心! (很抱歉,如果我没有尽我所能解释清楚,如果我听起来很蠢什么的。)

我想你可以让它更符合逻辑。

if message.content.startswith('BotName,'):
    #rest of the code

考虑一下,如果他们@你的机器人,字符串将是 <@1235574931>, do you think today will be a good day? 因此,只有当他们专门添加任何 BotName 时,它才会起作用。

另外,cog 监听器不需要 await self.client.process_commands(message)

您可以为您的机器人使用事件。尝试这样做。

@command.Cog.listener()
async def on_message(self, message, question):
    if message.content.startswith("8ball"):
        answers=["yes", "no", "maybe"]
        await message.channel.send(random.choice(answers)

不要忘记像这样导入随机数:

import random