async def remove_nickname(self, ctx: commands.Context, user: User, name: str): nicknames = NicknameUtil.get_all_nicknames(ctx.guild.id, user.id) if len(nicknames) < 1: await ctx.channel.send(f"{user.name}沒有任何暱稱.") return if not NicknameUtil.remove_nickname(ctx.guild.id, user.id, name): await ctx.channel.send(f"{name} 並不是 {user.name}的暱稱, 因此無法刪除.") return await ctx.channel.send(f"{user.name}的暱稱{name}刪除成功.")
async def remove_nickname_by_id(self, ctx: commands.Context, user: User, name_id: int): nicknames = NicknameUtil.get_all_nicknames(ctx.guild.id, user.id) if len(nicknames) < 1: await ctx.channel.send(f"{user.name}沒有任何暱稱.") return if not NicknameUtil.remove_nickname_id(ctx.guild.id, user.id, name_id): await ctx.channel.send(f"{name_id} 並不存在於 {user.name}的暱稱裡, 因此無法刪除.") return await ctx.channel.send(f"{user.name}的暱稱{name_id}刪除成功.")
async def get_nickname(self, ctx: commands.Context, user: User): nicknames = NicknameUtil.get_all_nicknames(ctx.guild.id, user.id) if len(nicknames) > 0: nickname_to_use = random.choice(nicknames) await ctx.channel.send(nickname_to_use) else: await ctx.channel.send(f"用戶'{user.name}'沒有任何暱稱.")
async def get_all_nickname(self, ctx: commands.Context, user: User): nicknames = NicknameUtil.get_all_nicknames(ctx.guild.id, user.id) if len(nicknames) < 1: await ctx.channel.send(f"{user.name}沒有任何暱稱.") return result = f"{user.name}有以下暱稱:\n" for index, nickname in enumerate(nicknames): result += f" {index + 1}.{nickname}" await ctx.channel.send(result)
async def set_nickname(self, ctx: commands.Context, user: User, name: str): if str(self.bot.user.id) in name: await ctx.channel.send(f"{name}不能當成暱稱使用") return result = NicknameUtil.set_nickname(ctx.guild.id, user.id, name) if result: await ctx.channel.send(f"新增用戶'{user.name}'新暱稱: {name} 成功!", ) else: await ctx.channel.send(f"用戶暱稱'{name}'已經存在.")
async def get_all_nickname_details(self, ctx: commands.Context, user: User): nicknames = NicknameUtil.get_all_nicknames_detail( ctx.guild.id, user.id) if len(nicknames) < 1: await ctx.channel.send(f"{user.name}沒有任何暱稱.") return result = f"{user.name}有以下暱稱:\n" nickname: NicknameModel for nickname in nicknames: result += f" {nickname.id}.{nickname.nick_name}" await ctx.channel.send(result)
async def clear_nickname(self, ctx: commands.Context, user: User): NicknameUtil.clear_nickname(ctx.guild.id, user.id) await ctx.channel.send(f"清除用戶'{user.name}'暱稱成功!", )
def test_setNicknames_clear(self): NicknameUtil.set_nickname(guild_id=1, user_id=1, nickname="hello") NicknameUtil.set_nickname(guild_id=1, user_id=1, nickname="world") NicknameUtil.clear_nickname(guild_id=1, user_id=1) nicknames = NicknameUtil.get_all_nicknames(guild_id=1, user_id=1) assert len(nicknames) == 0
def test_setNicknames_success(self): NicknameUtil.set_nickname(guild_id=1, user_id=1, nickname="hello") NicknameUtil.set_nickname(guild_id=1, user_id=1, nickname="world") nicknames = NicknameUtil.get_all_nicknames(guild_id=1, user_id=1) assert nicknames == ["hello", "world"]
def test_getNickname_empty(self): nicknames = NicknameUtil.get_all_nicknames(1, 2) assert len(nicknames) == 0