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

build.cogs.admincog.AdminCog.operation()   B

Complexity

Conditions 6

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 19
rs 8.6166
c 0
b 0
f 0
cc 6
nop 4
1
import asyncio
2
import discord
3
import cogs.cogbase as cogbase
4
from discord.ext import commands
5
from discord_slash import cog_ext, SlashContext
6
from cogs.databasecog import DatabaseCog
7
8
9
class AdminCog(cogbase.BaseCog):
10
    def __init__(self, base):
11
        super().__init__(base)
12
13
    # GENERAL FUNCTIONS
14
    # Check latency
15
    @cog_ext.cog_slash(name="ping", guild_ids=cogbase.GUILD_IDS,
16
                       description="Function for checking latency",
17
                       default_permission=False,
18
                       permissions=cogbase.PERMISSION_MODS)
19
    async def _ping(self, ctx: SlashContext):
20
        await ctx.send(f"Pong! {round(self.bot.latency * 1000)}ms", delete_after=4.0)
21
22
    # Clear messages
23
    @cog_ext.cog_slash(name="clear", guild_ids=cogbase.GUILD_IDS,
24
                       description="Function for clearing messages on channel",
25
                       default_permission=False,
26
                       permissions=cogbase.PERMISSION_MODS)
27
    async def _purge(self, ctx: SlashContext, number):
28
        num_messages = int(number)
29
        await ctx.channel.purge(limit=num_messages)
30
        await ctx.send(f"Cleared {num_messages} messages!", delete_after=4.0)
31
32
    # Disconnect Bot
33
    @cog_ext.cog_slash(name="exit", guild_ids=cogbase.GUILD_IDS,
34
                       description="Turn off the bot",
35
                       default_permission=False,
36
                       permissions=cogbase.PERMISSION_ADMINS)
37
    async def _exit(self, ctx: SlashContext):
38
        await ctx.send(f"Closing Bot", delete_after=1.0)
39
        dt_string = self.bot.get_current_time()
40
        print(f"({dt_string})\t[{self.__class__.__name__}]: Exiting Bot")
41
        await asyncio.sleep(3)
42
        await self.bot.close()
43
44
    # WARN FUNCTIONS
45
46
    # Warn user
47
    @cog_ext.cog_slash(name="warn", guild_ids=cogbase.GUILD_IDS,
48
                       description="Function for warning users",
49
                       default_permission=False,
50
                       permissions=cogbase.PERMISSION_MODS)
51
    async def _warn(self, ctx: SlashContext, user: discord.User, reason: str):
52
53
        await DatabaseCog.db_add_warn(user.id, reason)
54
        await ctx.send(
55
            f"{user.mention} was warned for:\n*\"{reason}\"*\n")  # f"Number of warns: {len(current_user['reasons'])}")
56
57
    # Get list of user's warns
58
    @cog_ext.cog_slash(name="warns", guild_ids=cogbase.GUILD_IDS,
59
                       description="Function for warning users",
60
                       default_permission=False,
61
                       permissions=cogbase.PERMISSION_MODS)
62
    async def _warns(self, ctx: SlashContext, user: discord.User):
63
        warns, nr_of_warns = await DatabaseCog.db_get_warns(user.id)
64
        nl = "\n"
65
        message = f"**{user.name}** has been warned **{nr_of_warns}** times\n\n_Reasons_:\n" \
66
                  f"{nl.join(warns)}\n"
67
        await ctx.author.send(message)
68
        await ctx.send(f"{user.name} warns has been sent to DM", hidden=True)
69
70
    # Remove all member's warns
71
    @cog_ext.cog_slash(name="removeWarns", guild_ids=cogbase.GUILD_IDS,
72
                       description="Function for removing user's all warns",
73
                       default_permission=False,
74
                       permissions=cogbase.PERMISSION_ADMINS)
75
    async def remove_warns(self, ctx: SlashContext, user: discord.User):
76
        await DatabaseCog.db_remove_warns(user.id)
77
        await ctx.send(f"{user.display_name}'s warns were deleted", hidden=True)
78
79
    # Mute member
80
    @cog_ext.cog_slash(name="mute", guild_ids=cogbase.GUILD_IDS,
81
                       description="Mute member for x minutes",
82
                       default_permission=False,
83
                       permissions=cogbase.PERMISSION_MODS)
84
    async def _mute(self, ctx: SlashContext, user: discord.User, mute_time: int, reason: str):
85
        duration = mute_time * 60
86
        guild = ctx.guild
87
        muted = discord.utils.get(guild.roles, name="Muted")
88
89
        if not muted:
90
            muted = await guild.create_role(name="Muted")
91
            for channel in guild.channels:
92
                await channel.set_permissions(muted, speak=False, send_messages=False, read_message_history=True,
93
                                              read_messages=False)
94
        await user.add_roles(muted, reason=reason)
95
        await ctx.send(f"{user.mention} Was muted by {ctx.author.name} for {mute_time} min\n"
96
                       f"Reason: {reason}", delete_after=10)
97
        await asyncio.sleep(duration)
98
        await user.remove_roles(muted)
99
        await ctx.send(f"{user.mention}'s mute is over", delete_after=10)
100
101
    # KICK FUNCTIONS
102
    @staticmethod
103
    async def operation(ctx, user: discord.Member, operation_t: str, reason=None):
104
        if user == ctx.author:
105
            return await ctx.send(
106
                f"{user.mention} You can't {operation_t} yourself", delete_after=5.0)
107
        if operation_t == "kick":
108
            await user.kick(reason=reason)
109
        elif operation_t == "ban":
110
            await user.ban(reason=reason)
111
        elif operation_t == "softban":
112
            await user.ban(reason=reason)
113
            await user.unban(reason=reason)
114
115
        if not reason:
116
            await ctx.send(f"{user} was {operation_t}ed", delete_after=10.0)
117
            await user.send(f"You were {operation_t}ed from {ctx.guild.name}")
118
        else:
119
            await ctx.send(f"{user} was {operation_t}ed\nReason: {reason}", delete_after=10.0)
120
            await user.send(f"You were {operation_t}ed from {ctx.guild.name}\nReason: {reason}")
121
122
    # Kick
123
    @cog_ext.cog_slash(name="kick", guild_ids=cogbase.GUILD_IDS,
124
                       description="Kicks member from the server",
125
                       default_permission=False,
126
                       permissions=cogbase.PERMISSION_MODS)
127
    async def kick(self, ctx, user: discord.Member, *, reason=None):
128
        operation_t = "kick"
129
        await self.operation(ctx, user, operation_t, reason)
130
131
    # Ban
132
    @cog_ext.cog_slash(name="ban", guild_ids=cogbase.GUILD_IDS,
133
                       description="Bans member from the server",
134
                       default_permission=False,
135
                       permissions=cogbase.PERMISSION_ADMINS)
136
    async def ban(self, ctx, user: discord.Member, *, reason=None):
137
        operation_t = "ban"
138
        await self.operation(ctx, user, operation_t, reason)
139
140
    # Softban
141
    @cog_ext.cog_slash(name="softban", guild_ids=cogbase.GUILD_IDS,
142
                       description="Bans and unbans the user, so their messages are deleted",
143
                       default_permission=False,
144
                       permissions=cogbase.PERMISSION_MODS)
145
    async def softban(self, ctx, user: discord.Member, *, reason=None):
146
        operation_t = "softban"
147
        await self.operation(ctx, user, operation_t, reason)
148
149
    # OTHER
150
    # Slow mode
151
    @cog_ext.cog_slash(name="slowmode", guild_ids=cogbase.GUILD_IDS,
152
                       description="Enable slowmode on current channel",
153
                       permissions=cogbase.PERMISSION_MODS)
154
    async def slowmode(self, ctx, seconds: int = 0):
155
        if seconds > 120:
156
            return await ctx.send(":no_entry: Amount can't be over 120 seconds")
157
        if seconds is 0:
158
            await ctx.channel.edit(slowmode_delay=seconds)
159
            a = await ctx.send("Slowmode is off for this channel")
160
            await a.add_reaction("a:redcard:871861842639716472")
161
        else:
162
            if seconds is 1:
163
                numofsecs = "second"
164
            else:
165
                numofsecs = "seconds"
166
            await ctx.channel.edit(slowmode_delay=seconds)
167
            confirm = await ctx.send(
168
                f"{ctx.author.display_name} set the channel slow mode delay to `{seconds}` {numofsecs}\n"
169
                f"To turn this off use /slowmode")
170
            await confirm.add_reaction("a:ResidentWitcher:871872130021736519")
171
172
173
def setup(bot: commands.Bot):
174
    bot.add_cog(AdminCog(bot))
175