0
votes

I'm trying to connect my discord bot to a voice channel, but it's not working.

There isn't any error or anything, nothing happens when I do !join on my discord channel.

Here is my code. I tried looking for some tutorials, but most seems outdated.

Could someone help me?

import discord
from discord.ext import commands
from discord.utils import get
import os

token = os.environ.get('DISCORD_TOKEN')
client = commands.Bot(command_prefix='!')


@client.command(pass_context=True)
async def join(ctx):
    channel = ctx.message.author.voice.channel
    voice = get(client.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()
    await ctx.send(f'Joined {channel}')

client.run(token)

Edit:

import discord
from discord.ext import commands
from discord.utils import get
import os
import youtube_dl

token = os.environ.get('DISCORD_TOKEN')
client = commands.Bot(command_prefix='!')
@client.event
async def on_message(message):
    channels = ['bot-commands']
    if str(message.channel) in channels:
        if message.content == '!help':
            embed = discord.Embed(title='How to use the ImposterBot', description='Useful Commands')
            embed.add_field(name='!help', value='Display all the commands')
            embed.add_field(name='!music', value='Play a music playlist')
            embed.add_field(name='!valorant', value='Get the most recent version of Valorant stats')
            embed.add_field(name='!hello', value='Greet a user')
            await message.channel.send(content=None, embed=embed)
        elif message.content == '!hello':
            await message.channel.send(f'Greeting, {message.author}!')
        elif message.content == '!valorant':
            await message.channel.send('This feature is not ready yet')
        elif message.content == '!music':
            await message.channel.send('This feature is not ready yet')
        elif 'sustin' in message.content:
            await message.channel.send(f"""it's sustin... {"<:monkas:392806765789446144>"} """)


@client.command(pass_context=True)
async def join(ctx):
    channel = ctx.message.author.voice.channel
    voice = get(client.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()
    await ctx.send(f'Joined {channel}')

client.run(token)
1
The command code works for me. Does the bot start at all? Could anything else in the code be causing problems besides the function invocation?Allister
@Allister oh really?? I'll try to comment out some chunck of code and test things out. Yes the bots connect and other commands like !help work fine.prayer_in_c
You have other code, then? Could you upload the full pastebin for me to see? There might not be anything I could find causing the issue but wouldn't hurt to look.Allister
@Allister, I edited.prayer_in_c
Add this to the top of your on_message event (right under async def on_message(message):, above the rest of the code: await bot.process_commands(message)Allister

1 Answers

0
votes

The error in this code is that commands will not be invoked by the bot if there is an on_message event, unless that event begins with this line:

async def on_message(message):
    await bot.process_commands(message)