Files

84 lines
3.1 KiB
Python

import discord
from redbot.core import commands, checks, Config
from redbot.core.bot import Red
class Renamer(commands.Cog):
"""Rename users with admin-only commands."""
def __init__(self, bot: Red):
self.bot = bot
self.config = Config.get_conf(self, identifier=182736451)
self.config.register_guild(allowed_roles=[])
@commands.group()
async def renamer(self, ctx: commands.Context):
"""Rename management commands."""
pass
@renamer.command(name="rename")
@commands.guild_only()
@commands.admin_or_permissions(administrator=True)
async def renamer_rename(self, ctx: commands.Context, member: discord.Member, *, nickname: str | None = None):
"""Rename a member. Leave nickname empty to clear it.
Only admins can use this command.
"""
try:
await member.edit(nick=nickname, reason=f"Renamed by {ctx.author}")
if nickname:
await ctx.send(f"Renamed {member.mention} to `{nickname}`.")
else:
await ctx.send(f"Cleared nickname for {member.mention}.")
except discord.Forbidden:
await ctx.send("I don't have permission to rename that member.")
except discord.HTTPException as e:
await ctx.send(f"Failed to rename member: {e}")
@renamer.command(name="bulkrename")
@commands.guild_only()
@commands.admin_or_permissions(administrator=True)
async def renamer_bulkrename(self, ctx: commands.Context, members: commands.Greedy[discord.Member], *, nickname: str):
"""Rename multiple members to the same nickname.
Only admins can use this command.
"""
if not members:
await ctx.send("You must specify at least one member to rename.")
return
renamed = 0
failed = 0
for member in members:
try:
await member.edit(nick=nickname, reason=f"Bulk renamed by {ctx.author}")
renamed += 1
except discord.Forbidden:
failed += 1
except discord.HTTPException:
failed += 1
await ctx.send(f"Renamed {renamed} member(s) to `{nickname}`. Failed: {failed}")
@renamer.command(name="massrename")
@commands.guild_only()
@commands.admin_or_permissions(administrator=True)
async def renamer_massrename(self, ctx: commands.Context, role: discord.Role, *, nickname: str):
"""Rename all members with a specific role to the same nickname.
Only admins can use this command.
"""
members = role.members
if not members:
await ctx.send("No members have that role.")
return
renamed = 0
failed = 0
for member in members:
try:
await member.edit(nick=nickname, reason=f"Mass renamed by {ctx.author}")
renamed += 1
except discord.Forbidden:
failed += 1
except discord.HTTPException:
failed += 1
await ctx.send(f"Renamed {renamed} member(s) with {role.mention} to `{nickname}`. Failed: {failed}")