discord.py-如何创建高级联接命令



我为我的discord.py机器人创建了一个join命令。用户可以指定机器人将连接的通道,也可以不指定,然后机器人将连接到用户的通道。但它不起作用。没有错误。它总是发送";错误找不到频道&";。

@bot.command()
async def join(ctx, *channelname):
if channelname is None:
try:
channel=ctx.author.voice.channel
await channel.connect()
await ctx.send("Joined!")
except AttributeError:
await ctx.send("Error! You are not connected to the channel available to me.")
except Exception as e:
print(e)
else:
channel = discord.utils.get(ctx.guild.channels, name=channelname, type="ChannelType.voice")
try:
await channel.connect()
await ctx.send("Joined!")
except AttributeError:
await ctx.send("Error! Channel not found.")
except Exception as e:
print(e)

您应该定义您的命令,使channelname = None。除此之外,通道的类型是一个对象,而不是字符串。

@bot.command()
async def join(ctx, *, channelname=None):
if channelname is None:
try:
channel = ctx.author.voice.channel
await channel.connect()
await ctx.send("Joined!")
except AttributeError:
await ctx.send("Error! You are not connected to the channel available to me.")
except Exception as e:
print(e)
else:
channel = discord.utils.get(
ctx.guild.channels, name=channelname, type=discord.ChannelType.voice)
try:
await channel.connect()
await ctx.send("Joined!")
except AttributeError:
await ctx.send("Error! Channel not found.")
except Exception as e:
print(e)

最新更新