我目前正在尝试 Discord Bots 。我在互联网上搜索了为什么此功能不起作用的答案,但找不到任何有效的答案。我想知道是否需要 opus库,还是仅是想播放声音? 我还没走那么远,所以我现在就不用担心播放声音了……我确实更改了 bot.join_voice_channel(channel)的代码,因为我看到了comment这样说是老方法。问题出在 join_channel 命令中,并且出在 await channel.connect()代码中。任何帮助将不胜感激!这是我的第一个问题,因此,如果我遗漏了一些东西,请告诉我...我打算让Discord Bot加入我所在的语音通道,但没有得到任何回应,也没有错误消息。它确实会打印“加入频道”,就是这样。
import os
from dotenv import load_dotenv
from discord.ext import commands
import discord
import random
import datetime
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix="!")
client = discord.Client()
@bot.event
async def on_ready():
print(f"{bot.user.name} has connected to Discord!")
@bot.command(name="speak", help="The bot speaks...")
async def say_something(ctx):
answers = [
"Hi there!",
"What's up?",
"How are you doing?"
]
response = random.choice(answers)
await ctx.send(response)
@bot.command(name="roll_dice", help="It rolles {x} number of dice with {y} amount of sides!")
async def roll(ctx, number_of_dice: int, number_of_sides: int):
dice = [
str(random.choice(range(1, number_of_sides + 1)))
for _ in range(number_of_dice)
]
await ctx.send(", ".join(dice))
@bot.command(name="create_channel", help="Creates a text channel!")
@commands.has_role("admin")
async def create_channel(ctx, channel_name):
guild = ctx.guild
existing_channel = discord.utils.get(guild.channels, name=channel_name)
if not existing_channel:
print(f"Creating a new channel: {channel_name}")
await guild.create_text_channel(channel_name)
@bot.command(name="time", help="Tells you the time...")
@commands.has_role("admin")
async def time(ctx):
current_time = datetime.datetime.strftime(datetime.datetime.now(), "%H:%M, %Y-%m-%d")
await ctx.send(current_time)
@bot.command(name="join_channel", help="Makes the bot join a channel!", aliases=["join"])
async def join(ctx):
print("Joining channel")
author = ctx.message.author
channel = author.voice.voice_channel
vc = await channel.connect(reconnect=True)
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.errors.CheckFailure):
await ctx.send("You do not have the correct role for this command...")
bot.run(TOKEN)