Passed
Branch main (f64b1b)
by Bartosz
01:11
created

ChannelUpCog.update_commons_ch_command()   A

Complexity

Conditions 5

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 17
rs 9.1832
c 0
b 0
f 0
cc 5
nop 2
1
import discord
2
import cogs.cogbase as cogbase
3
from discord.ext import commands
4
from discord_slash import cog_ext, SlashContext
5
6
7
class ChannelUpCog(cogbase.BaseCog):
8
    def __init__(self, base):
9
        super().__init__(base)
10
11
    # CHANNEL NAMES UPDATES
12
    # Total member channel name
13
    @cog_ext.cog_slash(name="updateTotalMembers", guild_ids=cogbase.GUILD_IDS,
14
                       description="Update total number of members",
15
                       default_permission=False,
16
                       permissions=cogbase.PERMISSION_MODS)
17
    async def update_member_count_command(self, ctx: SlashContext):
18
        await self.bot.update_member_count(ctx)
19
        await ctx.send(f"Total Members count updated", hidden=True)
20
21
    # Commons channel name
22
    @cog_ext.cog_slash(name="updateCommons", guild_ids=cogbase.GUILD_IDS,
23
                       description="Update common channel name",
24
                       default_permission=False,
25
                       permissions=cogbase.PERMISSION_MODS)
26
    async def update_commons_ch_command(self, ctx: SlashContext):
27
        with open('./server_files/commons.txt') as f:
28
            try:
29
                commons = f.read().splitlines()
30
            except ValueError:
31
                print(ValueError)
32
33
        await self.update_commons_ch(ctx, commons)
34
35
        commons.append(commons.pop(commons.index(commons[0])))
36
        with open('./server_files/commons.txt', 'w') as f:
37
            for item in commons:
38
                f.write("%s\n" % item)
39
40
    async def update_commons_ch(self, ctx: SlashContext, commons):
41
        new_name = f"common {commons[0]}"
42
        common_ch = self.bot.get_channel(self.bot.ch_common)
43
        await discord.TextChannel.edit(common_ch, name=new_name)
44
        dt_string = self.bot.get_current_time()
45
        print(f"({dt_string})\t[{self.__class__.__name__}]: Common channel name updated: {commons[0]}")
46
47
        await common_ch.send(f"Common changed: {commons[0]}")
48
        await ctx.send(f"Common changed: {commons[0]}", hidden=True)
49
50
    # N-Word spotted channel name
51
    # Doesn't work if used too many times in a short period of time
52
    @cog_ext.cog_slash(name="nword", guild_ids=cogbase.GUILD_IDS,
53
                       description="Change N-Word channel name",
54
                       permissions=cogbase.PERMISSION_ADMINS)
55
    async def rename_nword_channel(self, ctx, status: str):
56
        new_status = status
57
        channel = self.bot.get_channel(self.bot.ch_nightmare_killed)
58
        if new_status in channel.name:
59
            await ctx.send(f"{channel.name} has been changed", hidden=True)
60
        else:
61
            await discord.VoiceChannel.edit(channel, name=f"N-Word fixed: {new_status}")
62
            await ctx.send(f"{channel.name} channel name has been changed", hidden=True)
63
64
65
def setup(bot: commands.Bot):
66
    bot.add_cog(ChannelUpCog(bot))
67