我可以在discord.py中的多个函数中使用相同的变量吗?

问题描述 投票:0回答:1

我正在尝试制作一个机器人,它可以执行以下操作: 我想加入一家已经满员的VC 我运行一个命令 机器人制作了一个按钮 如果 vc 中的某个人按下按钮,我就会搬进去

我有这个代码: 对于 MoveUser 函数,我需要 movemeto 命令中的 whotomove 和 client 变量 如何将它们放入变量并在函数中使用它们? (另外,如果这个问题措辞不好,我很抱歉,我是新人)

async def MoveUser(interraction : discord.Interaction):
    channel = interraction.user.voice.channel
    user = interraction.user 
    print(channel)
    print(user) 
    await user.move_to(channel)


class MoveMenuButtons(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)


    @discord.ui.button(label="Accpet", style=discord.ButtonStyle.green)
    async def Accept(self, interraction:discord.Interaction, Button: discord.ui.Button,):
        await interraction.channel.send(content="accepted")
        await MoveUser(interraction,)

    @discord.ui.button(label="Decline", style=discord.ButtonStyle.red)
    async def Decline(self, interraction:discord.Interaction, Button: discord.ui.Button):
        await interraction.channel.send(content="declined")

@client.tree.command(name="movemeto", description="moves you to another voice channel")
async def movemeto(interraction : discord.Interaction, channel : discord.VoiceChannel):
    whotomove = interraction.user
    print (whotomove)
    print (channel)
    if(interraction.user.voice):
        await interraction.response.send_message(content=f"{whotomove} wants to be moved to the channel {channel}", view=MoveMenuButtons())
    else:
        await interraction.response.send_message("Join a voice channel to run this command")

我尝试将其作为参数传递(我认为它称为参数(括号中的东西)),但按钮似乎不支持它

python discord discord.py discord-buttons
1个回答
0
投票

您可以通过

__init__
方法传递这些变量并将它们保存在 MoveMenuButtons 视图中:

class MoveMenuButtons(discord.ui.View):
    def __init__(self, move_to: discord.VoiceChannel):
        self.move_to = move_to
        super().__init__(timeout=None)


    @discord.ui.button(label="Accpet", style=discord.ButtonStyle.green)
    async def Accept(self, interraction:discord.Interaction, Button: discord.ui.Button,):
        await interraction.channel.send(content="accepted")
        await self.interraction.user.move_to(self.channel)

    @discord.ui.button(label="Decline", style=discord.ButtonStyle.red)
    async def Decline(self, interraction:discord.Interaction, Button: discord.ui.Button):
        await interraction.channel.send(content="declined")
    

@client.tree.command(name="movemeto", description="moves you to another voice channel")
async def movemeto(interraction : discord.Interaction, channel : discord.VoiceChannel):
    whotomove = interraction.user
    print (whotomove)
    print (channel)
    if(interraction.user.voice):
        view = MoveMenuButtons(channel)
        await interraction.response.send_message(content=f"{whotomove} wants to be moved to the channel {channel}", view=view)
    else:
        await interraction.response.send_message("Join a voice channel to run this command")

© www.soinside.com 2019 - 2024. All rights reserved.