簡體   English   中英

如何使用戶在重新進入 discord 服務器時不會刪除靜音角色?

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

我為我的 discord.py 機器人創建了靜音和取消靜音命令,但如果靜音成員重新進入,角色將被刪除,就好像他沒有靜音一樣。 我怎樣才能使當你重新進入服務器時,它的角色仍然在他身上,用於術語 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

有一個名為on_member_join的事件,每次成員加入公會時都會觸發該事件。 一旦成員加入服務器,您可以使用此事件檢查成員當前是否根據您的機器人靜音。 您必須以某種方式跟蹤當前哪些成員被靜音。

以下是此代碼可以工作的基本方式。 我假設您可能可以使用列表作為存儲用戶靜音的一種方式。 這沒有經過測試,但大致是應該發生的。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM