简体   繁体   中英

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. 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. If you could help, id be grateful. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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