简体   繁体   English

Python中的Discord Bot-我试图从具有特定角色的所有人中随机挑选一个人

[英]Discord Bot in Python - Im trying to pick a random person from all the people with a specific role

So, i am trying to make a Discord Bot in Python. 因此,我正在尝试使用Python创建Discord Bot。 I'm just trying to make a bot that slaps a random person with the role "slapped". 我只是想制作一个可以打“打”角色的随机机器人。 I've gotten everything to work but i cant figure out how to call for a random member with the role. 我已经做好了所有准备工作,但我不知道该如何招募一个随机成员。 and if possible also limit it to who's currently online. 并尽可能将其限制为当前在线的用户。 I'm decently good at python but new to discord bots. 我相当擅长python,但对于不和谐的机器人还是陌生的。 If you could help, id be grateful. 如果您能帮忙,id会感激不尽。 My code 我的密码

import time
import discord
import random
from discord.ext import commands

bot = commands.Bot(command_prefix=',', description='Enjoy Being Slapped 
Randomly')
intervalM = random.randint(5,15)
#intervalS = integerM * 60

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)

@bot.command()
async def slapInt(integer):
    await bot.say("Time Between Slaps is now... " + str(integer) + "min")
    intervalM = integer

@bot.command()
"""manually slap someone"""
async def slapMan(intervalM, user):
    #debug - await bot.say("oof " + str(intervalM))
    intervalS = float(intervalM) * 60
    await bot.say("Slapping someone in " + str(intervalS) + " seconds")
    for i in range(0,int(intervalS)+1):
        if i == int(intervalS):
            await bot.say(" just got slapped!")
            intervalM = random.randint(5,15)
            slap(intervalM)
        else:
            #await bot.say(i)
            time.sleep(1)

def slap(intervalM):
    intervalS = float(intervalM) * 60
    print("Slapping someone in " + str(intervalS) + " seconds")
    for i in range(0,int(intervalS)+1):
        if i == int(intervalS):
            print(" just got slapped!")
            intervalM = random.randint(5,15)
            slap(intervalM)
        else:
            #await bot.say(i)
            time.sleep(1)

bot.run('~~~~~~~~~')
slap(intervalM)

You could try this: 您可以尝试以下方法:

server = discord.Server(id='your_server_id')

def slap(intervalM):
    intervalS = float(intervalM) * 60
    print("Slapping someone in " + str(intervalS) + " seconds")
    for i in range(0,int(intervalS)+1):
        if i == int(intervalS):
            roleMembers = []
            for member in server.members:
                for role in member.roles:
                    if role.name == 'your_role_name' and member.status == 'online':
                        roleMembers.append(member)
            memberCount = len(roleMembers)
            randomNumber = random.randint(0, (memberCount -1)
            await bot.say(roleMembers[randomNumber].name + ' just got slapped!')
            intervalM = random.randint(5,15)
            slap(intervalM)
        else:
            #await bot.say(i)
            time.sleep(1)

(code is not tested) (代码未经测试)

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

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