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



我为我的discord.py机器人创建了一个静音和取消静音命令,但如果被静音的成员将重新进入,该角色将被删除,就像他没有被静音一样。我该如何做到,当你重新进入服务器时,服务器的角色在剩下的时间里一直由他扮演?

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

最新更新