Passed
Push — main ( 530a31...c507f0 )
by Bartosz
02:42 queued 01:19
created

build.cogs.admincog.AdminCog._mute()   A

Complexity

Conditions 3

Size

Total Lines 20
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 20
rs 9.45
c 0
b 0
f 0
cc 3
nop 5
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="Check bot's latency",
17
                       default_permission=False,
18
                       permissions=cogbase.PERMISSION_MODS)
19
    async def check_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="Clear messages on channel",
25
                       default_permission=False,
26
                       permissions=cogbase.PERMISSION_MODS)
27
    async def purge_messages(self, ctx: SlashContext, number_to_delete: int = 1):
28
        messages = []
29
        async for message in ctx.channel.history(limit=number_to_delete + 1):
30
            messages.append(message)
31
        await ctx.channel.delete_messages(messages)
32
        await asyncio.sleep(5)
33
        await ctx.send(f"Cleared {number_to_delete} messages!", delete_after=3)
34
35
36
    # Disconnect Bot
37
    @cog_ext.cog_slash(name="exit", guild_ids=cogbase.GUILD_IDS,
38
                       description="Turn off the bot",
39
                       default_permission=False,
40
                       permissions=cogbase.PERMISSION_ADMINS)
41
    async def exit_bot(self, ctx: SlashContext):
42
        await ctx.send(f"Closing Bot", delete_after=1.0)
43
        self.create_log_msg("Exiting Bot")
44
        await asyncio.sleep(3)
45
        await self.bot.close()
46
47
    # WARN FUNCTIONS
48
49
    # Warn user
50
    @cog_ext.cog_slash(name="warn", guild_ids=cogbase.GUILD_IDS,
51
                       description="Warn member",
52
                       default_permission=False,
53
                       permissions=cogbase.PERMISSION_MODS)
54
    async def warn_user(self, ctx: SlashContext, user: discord.User, reason: str):
55
56
        await DatabaseCog.db_add_warn(user.id, reason)
57
        await ctx.send(
58
            f"{user.mention} was warned for:\n> {reason}\n")
59
60
    # Get list of user's warns
61
    @cog_ext.cog_slash(name="warns", guild_ids=cogbase.GUILD_IDS,
62
                       description="Get member warns",
63
                       default_permission=False,
64
                       permissions=cogbase.PERMISSION_MODS)
65
    async def user_warns(self, ctx: SlashContext, user: discord.User):
66
        warns, nr_of_warns = await DatabaseCog.db_get_warns(user.id)
67
        nl = "\n"
68
        message = f"**{user.name}** has been warned **{nr_of_warns}** times\n\n_Reasons_:\n" \
69
                  f"{nl.join(warns)}\n"
70
        await ctx.author.send(message)
71
        await ctx.send(f"{user.name} warns has been sent to DM", hidden=True)
72
73
    # Remove all member's warns
74
    @cog_ext.cog_slash(name="removeWarns", guild_ids=cogbase.GUILD_IDS,
75
                       description="Remove all member's warns",
76
                       default_permission=False,
77
                       permissions=cogbase.PERMISSION_ADMINS)
78
    async def remove_warns(self, ctx: SlashContext, user: discord.User):
79
        await DatabaseCog.db_remove_warns(user.id)
80
        await ctx.send(f"{user.display_name}'s warns were deleted", hidden=True)
81
82
    # Mute member
83
    @cog_ext.cog_slash(name="mute", guild_ids=cogbase.GUILD_IDS,
84
                       description="Mute member for x minutes",
85
                       default_permission=False,
86
                       permissions=cogbase.PERMISSION_MODS)
87
    async def mute_user(self, ctx: SlashContext, user: discord.User, mute_time: int, reason: str):
88
        duration = mute_time * 60
89
        guild = ctx.guild
90
        muted = discord.utils.get(guild.roles, name="Muted")
91
92
        if not muted:
93
            muted = await guild.create_role(name="Muted")
94
            for channel in guild.channels:
95
                await channel.set_permissions(muted, speak=False, send_messages=False, read_message_history=True,
96
                                              read_messages=False)
97
        await user.add_roles(muted, reason=reason)
98
        await ctx.send(f"{user.mention} Was muted by {ctx.author.name} for {mute_time} min\n"
99
                       f"Reason: {reason}", delete_after=10)
100
        await asyncio.sleep(duration)
101
        await user.remove_roles(muted)
102
        await ctx.send(f"{user.mention}'s mute is over", delete_after=10)
103
104
    # KICK FUNCTIONS
105
    @staticmethod
106
    async def operation(ctx, user: discord.Member, operation_t: str, reason=None):
107
        if user == ctx.author:
108
            return await ctx.send(
109
                f"{user.mention} You can't {operation_t} yourself", delete_after=5.0)
110
        if operation_t == "kick":
111
            await user.kick(reason=reason)
112
        elif operation_t == "ban":
113
            await user.ban(reason=reason)
114
        elif operation_t == "softban":
115
            await user.ban(reason=reason)
116
            await user.unban(reason=reason)
117
        reason_str = f"\nReason: {reason}" if reason else ""
118
119
        await ctx.send(f"{user} was {operation_t}ed{reason_str}")
120
        await user.send(f"You were {operation_t}ed from {ctx.guild.name}{reason_str}")
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="Ban 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="Ban and unban 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
                       default_permission=False,
154
                       permissions=cogbase.PERMISSION_MODS)
155
    async def slowmode(self, ctx, seconds: int = 0):
156
        if seconds > 120:
157
            return await ctx.send(":no_entry: Amount can't be over 120 seconds")
158
        if seconds == 0:
159
            await ctx.channel.edit(slowmode_delay=seconds)
160
            a = await ctx.send("Slowmode is off for this channel")
161
            await a.add_reaction("a:redcard:871861842639716472")
162
        else:
163
            if seconds == 1:
164
                numofsecs = "second"
165
            else:
166
                numofsecs = "seconds"
167
            await ctx.channel.edit(slowmode_delay=seconds)
168
            confirm = await ctx.send(
169
                f"{ctx.author.display_name} set the channel slow mode delay to `{seconds}` {numofsecs}\n"
170
                f"To turn this off use /slowmode")
171
            await confirm.add_reaction("a:ResidentWitcher:871872130021736519")
172
173
174
def setup(bot: commands.Bot):
175
    bot.add_cog(AdminCog(bot))
176