我正在为一个朋友编写一个机器人,他们让我发出一个 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))
如果不可能,那就别担心! (对不起,如果我没有尽可能地解释清楚,如果我听起来很笨或什么的。)
答案 0 :(得分:0)
我想你可以用更多的逻辑让它工作。
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)
答案 1 :(得分:0)
您可以为您的机器人使用事件。尝试这样做。
@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
import choice