简体   繁体   中英

How can I make it so that the user does not remove mute role when re-entering the discord server?

I created a mute and unmute command for my discord.py bot, but if muted member will re-enter, the role will be removed as if he was not muted. How can I make it so that when you re-enter the server, its role remains with him for the rest of the term?

import discord
from discord.ext import commands
import asyncio

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix="/", intents = intents)

logchannel = bot.get_channel(**********)

@bot.command()
async def mute(ctx, member:discord.Member, time:int):
    muterole = discord.utils.get(ctx.guild.roles, id = *********)
    await member.add_roles(muterole)
    await asyncio.sleep(time * 60)
    if muterole in member.roles:
        await member.remove_roles(muterole)
        return
    else:
        return

@bot.command()
async def unmute(ctx, member:discord.Member):
    muterole = discord.utils.get(ctx.guild.roles, id = ******)
    if muterole in member.roles:
        await member.remove_roles(muterole)
        return
    else:
        return

There is an event called on_member_join which is triggered every single time a member joins a guild. You could use this event to check if a member is currently muted according to your bot once they join a server. You would have to keep track of which members are currently muted in some sort of way.

Below is a basic way that this code could work. I assumed that you could probably use a list as a way to store user mutes. This wasn't tested but it's roughly what should happen.

import discord
from discord.ext import commands
import asyncio

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix="/", intents = intents)

logchannel = bot.get_channel(**********)

list_of_muted_members = []

@bot.event
async def on_member_join(member):
     if (member in list_of_muted_members): # Once someone joins the server, this event gets triggered and checks to see if the person is currently in the muted list, if they are, the muted role gets given back to them.
          muterole = discord.utils.get(member.guild.roles, name = "muterole")
          await member.add_roles(muterole)

@bot.command()
async def mute(ctx, member:discord.Member, time:int):
    muterole = discord.utils.get(ctx.guild.roles, id = *********)
    await member.add_roles(muterole)
    list_of_muted_members.append(member) # This will add the user to the list because they muted

    await asyncio.sleep(time * 60)
    if muterole in member.roles:
        await member.remove_roles(muterole)
        list_of_muted_members.remove(member) # This will remove the user from the list because they aren't muted anymore
        return
    else:
        return

@bot.command()
async def unmute(ctx, member:discord.Member):
    muterole = discord.utils.get(ctx.guild.roles, id = ******)
    if muterole in member.roles:
        await member.remove_roles(muterole)
        list_of_muted_members.remove(member) # This will remove the user from the list because they aren't muted anymore
        return
    else:
        return

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