discord.py 8ball命令,但不带bot前缀

t9eec4r0  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(195)

我正在为一个朋友编写一个机器人程序,他们让我执行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))

如果不可能,那就别担心(对不起,如果我没有尽我所能解释清楚,如果我听起来很蠢或什么的话。)

3npbholx

3npbholx1#

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

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)

kpbpu008

kpbpu0082#

您可以为bot使用事件。试着这样做。

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

别忘了像这样导入random和choice:

import random
import choice

相关问题