我正在尝试制造一个不和谐的机器人。它可以很好地响应某些消息,这是我想要执行的操作的一部分。我现在希望它能够加入语音频道,但不会:
import discord
import youtube_dl
from discord.ext import commands
from discord.utils import get
TOKEN = 'token string here
BOT_PREFIX = '/'
bot = commands.Bot(command_prefix=BOT_PREFIX)
players = {}
@bot.event
async def on_ready():
print(bot.user.name + ' is now online.\n')
然后在我的邮件处理完@ bot.event之后,
@bot.command(pass_context=True)
async def join(ctx):
print('Join executed')
global voice
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
print (f"The bot has joined {channel}\n")
@bot.command(pass_context=True)
async def leave(ctx):
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.disconnect()
print(f"The bot has left {channel}")
else:
print("Bot was asked to leave, but was not in one.")
我很新,所以它可能(或者可能是)非常简单,愚蠢的修复程序。
甚至没有处理join命令,因为当我在不和谐聊天中调用它时,该打印语句未在控制台中显示。
我正在跟踪this video tutorial到T,他显然可以正常工作。
答案 0 :(得分:0)
我认为您刚收到on_message事件。如果您这样做,请选中此docs page。
另外我也不知道为什么您通过了pass_context
,他们在1.0版中将其删除,而现在甚至不存在。
答案 1 :(得分:0)
在下面检查。这将与最新版本的discord.py兼容。
@bot.command(name='join', invoke_without_subcommand=True)
async def join(ctx):
destination = ctx.author.voice.channel
if ctx.voice_state.voice:
await ctx.voice_state.voice.move_to(destination)
return
ctx.voice_state.voice = await destination.connect()
await ctx.send(f"Joined {ctx.author.voice.channel} Voice Channel")