【问题标题】:Discord.py make 'wait_for' to only work in private messagesDiscord.py 使“wait_for”仅在私人消息中工作
【发布时间】:2020-07-26 03:06:49
【问题描述】:

我在制作这个不和谐机器人时遇到了一个问题。每当用户运行命令!!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))

【问题讨论】:

  • 加个条件看看公会是否为None
  • 我该怎么做? if guild.message == None:?一直在阅读文档,但找不到我要查找的内容。

标签: python python-3.x discord.py discord.py-rewrite


【解决方案1】:

您可以检查频道是否为私人频道,例如if isinstance(message.channel, discord.DMChannel)

【讨论】:

    猜你喜欢
    • 2020-09-16
    • 2018-09-19
    • 1970-01-01
    • 2019-07-10
    • 2020-11-27
    • 2021-06-27
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多