制作这个不一致的机器人时遇到了一个问题。每当用户运行命令!!bug
时,都会有一条私人消息,用户必须回答一些问题。问题在于,无论何时运行命令,您仍然可以在运行命令的聊天中回答问题。
我希望它仅在用户写私人消息时才等待用户响应,而不是在命令运行时进行的聊天。所以这行代码:
responseDesc = await self.client.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
仅在用户写私人消息时才起作用。
这是我的代码:
import discord
from discord.ext import commands
import asyncio
emojis = ["\u2705", "\U0001F6AB", "\u274C"]
class Bug(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def bug(self, ctx, desc=None, rep=None):
await ctx.channel.purge(limit=1)
user = ctx.author
await ctx.author.send('```Please explain the bug```')
responseDesc = await self.client.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
description = responseDesc.content
await ctx.author.send('```Please provide pictures/videos of this bug```')
responseRep = await self.client.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
replicate = responseRep.content
await ctx.author.send('Your bug report has been sent.')
embed = discord.Embed(title='Bug Report', color=0x00ff00)
embed.add_field(name='Description',
value=description, inline=False)
embed.add_field(name='Replicate', value=replicate, inline=True)
embed.add_field(name='Reported By', value=user, inline=True)
adminBug = self.client.get_channel(733721953134837861)
message = await adminBug.send(embed=embed)
for emoji in emojis:
await message.add_reaction(emoji)
@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
reaction_message = reaction.message
message = await reaction_message.channel.fetch_message(reaction_message.id)
my_embed = message.embeds[0]
emoji = reaction.emoji
if user.bot:
return
if emoji == "\u2705":
fixed_channel = self.client.get_channel(733722567449509958)
await fixed_channel.send(embed=my_embed)
elif emoji == "\U0001F6AB":
notBug = self.client.get_channel(733722584801083502)
await notBug.send(embed=my_embed)
elif emoji == "\u274C":
notFixed = self.client.get_channel(733722600706146324)
await notFixed.send(embed=my_embed)
else:
return
def setup(client):
client.add_cog(Bug(client))
答案 0 :(得分:1)
您可以检查频道是否为私密频道,例如if isinstance(message.channel, discord.DMChannel)