[英]Discord.py bot responds to 2 if statements at the same time
I am writing a leave command for my music module for my discord.py bot.我正在为我的 discord.py 机器人的音乐模块编写离开命令。 I want the bot to say a message when someone uses >leave
when the bot is not in a VC.当机器人不在 VC 中时,我希望机器人在有人使用>leave
时说出一条消息。 I tried to write it out below but whenever the bot is in a VC and I type >leave
, it says both messages below, but this does not happen when the bot is not in a VC.我试图在下面写出来,但是每当机器人在 VC 中并且我键入>leave
时,它都会在下面显示两条消息,但是当机器人不在 VC 中时不会发生这种情况。 I do not get any errors at all.我根本没有收到任何错误。 My question is, how can I get my bot to say, "I'm not even in a VC you weirdo."我的问题是,我怎样才能让我的机器人说:“我什至不在 VC 中,你这个怪人。” when someone uses >leave
only when the bot is not is a VC?当有人使用>leave
时,只有当机器人不是 VC 时?
@bot.command()
async def leave(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice.is_connected():
await voice.disconnect()
await ctx.send("I have left the VC.")
if not voice.is_connected():
await ctx.send("I'm not even in a VC you weirdo.")
The problem is that when the await voice.disconnect()
is executed, the voice.is_connected()
will output false, because the bot is indeed disconnected from your previous command, so the if statement below the first if statement will get executed.问题是当await voice.disconnect()
执行时, voice.is_connected()
会 output false,因为 bot 确实与你之前的命令断开连接,所以第一个 if 语句下面的 if 语句将被执行。 There are two ways that you can fix it, one recommended way is to use elif
, and the other would be switching the if statements around:有两种方法可以修复它,一种推荐的方法是使用elif
,另一种方法是切换 if 语句:
elif
example elif
@bot.command()
async def leave(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice.is_connected():
await voice.disconnect()
await ctx.send("I have left the VC.")
elif not voice.is_connected():
await ctx.send("I'm not even in a VC you weirdo.")
switching around example切换示例
@bot.command()
async def leave(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if not voice.is_connected():
await ctx.send("I'm not even in a VC you weirdo.")
if voice.is_connected():
await voice.disconnect()
await ctx.send("I have left the VC.")
As knosmos pointed out in the comment you can also just use the else statement:正如 knosmos 在评论中指出的那样,您也可以只使用 else 语句:
@bot.command()
async def leave(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice.is_connected():
await voice.disconnect()
await ctx.send("I have left the VC.")
else:
await ctx.send("I'm not even in a VC you weirdo.")
You could also use an else
statement instead of elif
to make the code a bit cleaner:您还可以使用else
语句而不是elif
来使代码更简洁:
@bot.command()
async def leave(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice.is_connected():
await voice.disconnect()
await ctx.send("I have left the VC.")
else:
await ctx.send("I'm not even in a VC you weirdo.")
Many people have explained what to do in previous answers.许多人已经在之前的答案中解释了要做什么。 My below code should help you better understand your current logic.我下面的代码应该可以帮助您更好地理解您当前的逻辑。
x = True
if x:
print('True')
x = False
if not x:
print('False')
First, we set x to True.首先,我们将 x 设置为 True。 We then check if x is true.然后我们检查 x 是否为真。 If so we can print that, and set x to false.如果是这样,我们可以打印它,并将 x 设置为 false。 Lastly we will check if x is false and sure enough it is.最后,我们将检查 x 是否为假,并且确实如此。 So we print false.所以我们打印错误。
The same thing is happening with you.同样的事情也发生在你身上。 You check if you are connected, if so you leave.你检查你是否连接,如果是,你离开。 Then you check if you are not in a vc.然后你检查你是否不在vc中。
So like many people have said, you either use an elif statement to check if you are not in a vc or you can use an else statement.所以就像很多人说的那样,你要么使用 elif 语句来检查你是否不在 vc 中,要么你可以使用 else 语句。
correct正确的
x = True
if x:
print('True')
x = False
elif not x:
print('False')
`
You are disconnecting the bot and checking if the bot is connected to any VC one after the other...您正在断开机器人并检查机器人是否一个接一个地连接到任何 VC...
Use elif
使用elif
Your current code: if the bot is in a VC您当前的代码:如果机器人在 VC 中
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild) #gets VC ID
if voice.is_connected(): #This condition is true
await voice.disconnect() #after this, voice.is_connected() is false
await ctx.send("I have left the VC.") #print that
elif not voice.is_connected(): #After the above, this is also true, use elif here
await ctx.send("I'm not even in a VC you weirdo.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.