简体   繁体   English

如何使用户在重新进入 discord 服务器时不会删除静音角色?

[英]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.我为我的 discord.py 机器人创建了静音和取消静音命令,但如果静音成员重新进入,角色将被删除,就好像他没有静音一样。 How can I make it so that when you re-enter the server, its role remains with him for the rest of the term?我怎样才能使当你重新进入服务器时,它的角色仍然在他身上,用于术语 rest?

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.有一个名为on_member_join的事件,每次成员加入公会时都会触发该事件。 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

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

相关问题 如果还没有静音角色,如何使静音命令添加静音角色? (discord.py) - How can I make the mute command add a Muted role if there isn't already one? (discord.py) 我如何制作它以读取用户所说的内容并使用它来扮演角色? discord.py - How do i make it so it reads what the user says and uses it to make a role? discord.py Python,重新进入`with`块 - Python, re-entering the `with` block Discord Py / 如何从服务器用户中删除特定角色 - Discord Py / How to remove a specific role from a server user 在 Python 中编程 Discord 机器人 - 我将如何制作它以便我的静音命令是定时的? - Programming a Discord bot in Python- How would I make it so my mute command is timed? discord py 如何为具有 id 的服务器成员用户提供角色 - discord py how can i give a role to server member user with id 一旦用户获得另一个角色,如何让机器人删除角色? [不和谐.py] - How to make bot remove role once user gets another role? [discord.py] 我如何知道用户何时在 Discord.py 中加入 Discord(Discord 加入日期,而不是服务器) - How can I know when a user joined Discord in Discord.py (Discord join date, not server) 当用户加入服务器时,我如何给用户一个角色? - How i can give a role to user when its join the server? 是否有退出和重新输入脚本的命令? - Is there a command for exiting and re-entering a script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM