Discord机器人未加入语音通道(Python)

时间:2020-09-19 02:51:13

标签: python discord bots

我正在尝试将Discord机器人连接到语音通道,但无法正常工作。

没有任何错误或任何东西,当我在不和谐的频道上加入时什么也没发生。

这是我的代码。我尝试寻找一些教程,但大多数似乎已过时。

有人可以帮我吗?

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)

编辑:

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 个答案:

答案 0 :(得分:0)

此代码中的错误是,如果有on_message事件,则机器人将不会调用命令,除非该事件以以下行开头:

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