Search code examples
pythondiscorddiscord.pydiscord-buttons

Can I use the same variable in multiple functions in discord.py?


I am trying to make a bot, that can do the following: I want to join a vc that is full I run a command the bot makes a button If someone that is in the vc presses the button, I get moved in

I have this code: For the MoveUser function I need the whotomove and client variables from the movemeto command How can I get them into a variable and use them in the function? (also, apologies if this question is badly phrased, I am quite new)

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")

I attempted to pass it as (I think it's called a parameter(the thing in the bracket)) but the buttons don't seem to support it


Solution

  • You can pass these variables and save them in your MoveMenuButtons view through the __init__ method:

    class MoveMenuButtons(discord.ui.View):
        def __init__(self, to_channel: discord.VoiceChannel):
            self.to_channel = to_channel
            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.response.edit_message(content="accepted", view=None)
            await self.interraction.user.move_to(self.to_channel)
    
        @discord.ui.button(label="Decline", style=discord.ButtonStyle.red)
        async def decline(self, interraction:discord.Interaction, button: discord.ui.Button):
            await interraction.response.edit_message(content="declined", view=None)
        
    
    @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")