[Python] How to write a discord Auto-voice bot on python disnake?

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024.

  1. Stack

    Stack Membro Participativo

    I'm making a discord bot on python using disnake, but because I'm very new to this, I don't understand how to do what I want. The idea is to have one voice channel called "Create voice chat" and when user joines it, bot automatically creates new voice channel and moves that user to it. After there's no one in the created channel, it destroys.

    I've found what I've need on autocode.com, but I can't figure out how to rewrite this on python using disnake. Any ideas?

    That code from autocode(js)

    const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
    const event = context.params.event;
    const { guild_id, channel_id, user_id } = event

    // The 'central' voice channel users must join first
    const voiceChannelId = process.env.VOICE_CHANNEL_ID

    // The voice channel name to create for the user
    const voiceChannelName = `vc_${user_id}`

    // Create a new voice channel when the user joins the specifc voice channel
    const joinedChannel = channel_id === voiceChannelId
    if (joinedChannel) {
    const channel = await lib.discord.guilds['@0.1.0'].channels.create({
    guild_id,
    name: voiceChannelName,
    type: 2, // vc
    })
    await lib.discord.guilds['@0.1.0'].members.update({
    guild_id, user_id,
    channel_id: channel.id,
    });
    return
    }

    // Delete the user's voice channel when the user disconnects
    const leftChannel = !channel_id
    if (leftChannel) {
    const channels = await lib.discord.guilds['@0.1.0'].channels.list({ guild_id });
    const channel = channels.find(c => c.name === voiceChannelName)
    if (channel)
    await lib.discord.channels['@0.2.0'].destroy({ channel_id: channel.id });
    return
    }


    What I've done by myself(not working)

    def _get_channel(self) -> disnake.abc.GuildChannel:
    return self.channel


    def __init__(self):
    self.auto_channel = 1030505142820282408
    self.auto_channel = self.guild.get_channel(self.auto_channel)


    @bot.event
    async def on_voice_state_update(self, member = disnake.Member, before = disnake.VoiceState, after = disnake.VoiceState) -> None:
    if after.channel is not None and after.channel == auto_channel:
    ch = await self.auto_channel.category.create_voice_channel(name=member.display_name, overwrites={**self.auto_channel.category.overwrites, member: disnake.PermissionOverwrite(connect=True, move_members=True, manage_channels=True, manage_roles=True)})
    try:
    await member.move_to(ch, reason='autochannel')
    except disnake.HTTPException:
    pass
    if before.channel is not None and before.channel.id != auto_channel and before.channel.category.id == auto_channel and len(before.channel.members) == 0:
    try:
    await before.channel.delete()
    except disnake.NotFound:
    pass

    Continue reading...

Compartilhe esta Página