简体   繁体   English

在 discord.py 中使用随机模块

[英]using random module in discord.py

with start command in discord Im getting a list of users from author voice channel and store them inside a list and remove song bots and other relevant bots from this list cause I need only users(persons not bots).使用 discord 中的启动命令我从作者语音频道获取用户列表并将它们存储在列表中并从该列表中删除歌曲机器人和其他相关机器人,因为我只需要用户(人不是机器人)。 after change their id to their names mention one of them randomly and save that inside a varriable (userchoosed) like this:将他们的 id 更改为他们的名字后,随机提及其中一个并将其保存在一个变量(用户选择)中,如下所示:

userchoosed = ""
@client.command()
async def start(ctx):
    channelVoice = ctx.author.voice.channel #getting author voice channel


    member_ids = channelVoice.members #finds members connected to the channel

    ids = [] #empty list
    for member in member_ids:
        ids.append(member.mention) #change the member's id to their names and list them in ids[]
    
    listToRemove = ['<@!547905866255433758>','<@!234395307759108106>','<@!614109280508968980>','<@!806618501041225728>','<@!228537642583588864>','<@!184405311681986560>','<@!428655825817567261>']
    finalList = list(set(ids) - set(listToRemove)) #final list is a list without bots with above ids
    print(finalList) #just to see the result until this line of code
    await ctx.send("`` Lets go! ``")
    userchoosed = random.choice(finalList)
    await ctx.send(userchoosed)
    global listTouse #making a global list of Final list to use it again in other command
    listTouse = finalList
    

ok now we have a variable with a random user.好的,现在我们有一个随机用户的变量。 now I want to mention another user but not equal the previous user cause I dont want a user to be called twise in a row randomly so I make it by this:现在我想提及另一个用户,但不等于前一个用户,因为我不希望一个用户被随机连续调用两次,所以我这样做:

@client.command()
async def next(ctx):
    user = random.choice(listTouse) #making this variable for storing another user from list
    while userchoosed == user: #check if this random user equal userchoosed(the previous user)
        user = random.choices(listTouse) #again store another random user
        userchoosed == user #change the userchoosed to user for the next time member use this command
    await ctx.send(user)#and finally mention user

I tried these but again I get same user mention twise in a row What is the problem??我尝试了这些,但我再次得到相同的用户连续两次提及有什么问题?

In the second code snippet you want to change userchoosed to user , but you actually change user back to userchoosed in the second last line.在第二个代码片段中,您想将userchoosed更改为user ,但实际上您在倒数第二行将user更改回userchoosed Changing this should give you the result you want.改变它应该会给你你想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM