简体   繁体   English

如何让这个命令同时在不同的地方工作?? 不和谐.py

[英]How do I get this command to work in different places at the same time?? discord.py

I want this command to work in different places at the same time.我希望这个命令可以同时在不同的地方工作。 When I run it on one channel, and my friend runs it on another channel, the command starts to be duplicated when one of us presses the button.当我在一个频道上运行它,而我的朋友在另一个频道上运行它时,当我们中的一个人按下按钮时,命令开始重复。 I don't click anything, but if my friend clicks on the button in his channel, the message will be sent in both channels.我没有点击任何东西,但是如果我的朋友点击他频道中的按钮,消息将在两个频道中发送。

import random
import string
import discord
from discord_components import DiscordComponents, Button, ButtonStyle

@bot.command()
async def random_screenshot(ctx):
    while True:
        letters = string.ascii_lowercase + string.digits
        rand_string = ''.join(random.choice(letters) for i in range(6))
        link = "https://prnt.sc/" + str(rand_string)

        await ctx.send(link,
                                 components=[
                                     Button(style=ButtonStyle.blue, label="Next", emoji="➡")])

        await bot.wait_for('button_click')

This usually happens with all commands when i use the while loop当我使用 while 循环时,这通常发生在所有命令中

The while loop isn't the problem here (though it's a separate problem). while 循环不是这里的问题(尽管它是一个单独的问题)。

What's happening is that await bot.wait_for("button_click") doesn't care what button is pressed.发生的事情是await bot.wait_for("button_click")不关心按下了什么按钮。 This means that when the command is run twice, then a button is clicked, both messages respond.这意味着当命令运行两次,然后单击一个按钮时,两个消息都会响应。

You'll want to make sure that the wait_for only continues if the button is our message's button (instead of another message's button).您需要确保只有当按钮是我们消息的按钮(而不是另一个消息的按钮)时, wait_for才会继续。 To do that, you'll need to do two things.为此,您需要做两件事。

First, we need to generate a random string and set our button's custom id to it.首先,我们需要生成一个随机字符串并将按钮的自定义 id 设置为它。 This is so that we can know which button was pressed depending on its custom id.这样我们就可以根据自定义 id 知道按下了哪个按钮。 But wait, the discord_components library already generates an ID for us when we create the Button object, so we can just remember its ID like so:但是等等,当我们创建Button对象时,discord_components 库已经为我们生成了一个 ID,所以我们可以像这样记住它的 ID:

button = Button(style=ButtonStyle.blue, label="Next", emoji="➡")
button_id = button.custom_id
await ctx.send(link, components=[button])

Second, we'll need to pass a function to wait_for as the check keyword argument that only returns True if the button clicked was our button.其次,我们需要将一个函数传递给wait_for作为 check 关键字参数,如果单击的按钮是我们的按钮,则该函数仅返回 True。 Something like so:像这样:

def check_interaction(interaction):
    return interaction.custom_id == button_id
await bot.wait_for("button_click", check=check_interaction)

Now your command should only respond to its own button presses :D现在你的命令应该只响应它自己的按钮按下:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM