【发布时间】:2020-12-30 23:38:57
【问题描述】:
我正在尝试制作一个不和谐的机器人。它可以很好地响应某些消息,这是我想要它做的一部分。我现在希望它能够加入语音频道,但它不会:
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.")
我是新手,所以它可能是(或可能是)一个非常简单、愚蠢的解决方法。
连接命令甚至没有被处理,因为当我在不和谐聊天中调用它时,该打印语句没有显示在控制台中。
我正在关注 this video tutorial 到 T,他显然工作正常。
【问题讨论】:
-
您的代码对我有用。您是否在机器人可以看到的文本频道中调用
/join?除了/join,其他命令是否有效? (尝试制作一个仅打印到控制台的简单命令) -
你有
on_message()活动吗? -
我确实有一个 on_message 事件,它工作得非常好。 Allister,我会尝试一些其他简单的命令并回复您。
标签: python discord discord.py discord.py-rewrite