Passed
Branch BonHowi (9e2aee)
by Bartosz
01:36
created

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

Complexity

Conditions 3

Size

Total Lines 20
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nop 5
dl 0
loc 20
rs 9.45
c 0
b 0
f 0
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
    # Disconnect Bot
36
    @cog_ext.cog_slash(name="exit", guild_ids=cogbase.GUILD_IDS,
37
                       description="Turn off the bot",
38
                       default_permission=False,
39
                       permissions=cogbase.PERMISSION_ADMINS)
40
    async def exit_bot(self, ctx: SlashContext):
41
        await ctx.send(f"Closing Bot", delete_after=1.0)
42
        self.create_log_msg("Exiting Bot")
43
        await asyncio.sleep(3)
44
        await self.bot.close()
45
46
    # WARN FUNCTIONS
47
48
    # Warn user
49
    @cog_ext.cog_slash(name="warn", guild_ids=cogbase.GUILD_IDS,
50
                       description="Warn member",
51
                       default_permission=False,
52
                       permissions=cogbase.PERMISSION_MODS)
53
    async def warn_user(self, ctx: SlashContext, user: discord.User, reason: str):
54
55
        await DatabaseCog.db_add_warn(user.id, reason)
56
        await ctx.send(
57
            f"{user.mention} was warned for:\n> {reason}\n")
58
59
    # Get list of user's warns
60
    @cog_ext.cog_slash(name="warns", guild_ids=cogbase.GUILD_IDS,
61
                       description="Get member warns",
62
                       default_permission=False,
63
                       permissions=cogbase.PERMISSION_MODS)
64
    async def user_warns(self, ctx: SlashContext, user: discord.User):
65
        warns, nr_of_warns = await DatabaseCog.db_get_warns(user.id)
66
        nl = "\n"
67
        message = f"**{user.name}** has been warned **{nr_of_warns}** times\n\n_Reasons_:\n" \
68
                  f"{nl.join(warns)}\n"
69
        await ctx.author.send(message)
70
        await ctx.send(f"{user.name} warns has been sent to DM", hidden=True)
71
72
    # Remove all member's warns
73
    @cog_ext.cog_slash(name="removeWarns", guild_ids=cogbase.GUILD_IDS,
74
                       description="Remove all member's warns",
75
                       default_permission=False,
76
                       permissions=cogbase.PERMISSION_ADMINS)
77
    async def remove_warns(self, ctx: SlashContext, user: discord.User):
78
        await DatabaseCog.db_remove_warns(user.id)
79
        await ctx.send(f"{user.display_name}'s warns were deleted", hidden=True)
80
81
    # Mute member
82
    @cog_ext.cog_slash(name="mute", guild_ids=cogbase.GUILD_IDS,
83
                       description="Mute member for x minutes",
84
                       default_permission=False,
85
                       permissions=cogbase.PERMISSION_MODS)
86
    async def mute_user(self, ctx: SlashContext, user: discord.User, mute_time: int, reason: str):
87
        duration = mute_time * 60
88
        guild = ctx.guild
89
        muted = discord.utils.get(guild.roles, name="Muted")
90
91
        if not muted:
92
            muted = await guild.create_role(name="Muted")
93
            for channel in guild.channels:
94
                await channel.set_permissions(muted, speak=False, send_messages=False, read_message_history=True,
95
                                              read_messages=False)
96
        await user.add_roles(muted, reason=reason)
97
        await ctx.send(f"{user.mention} Was muted by {ctx.author.name} for {mute_time} min\n"
98
                       f"Reason: {reason}", delete_after=10)
99
        await asyncio.sleep(duration)
100
        await user.remove_roles(muted)
101
        await ctx.send(f"{user.mention}'s mute is over", delete_after=10)
102
103
    # KICK FUNCTIONS
104
    @staticmethod
105
    async def operation(ctx, user: discord.Member, operation_t: str, reason=None):
106
        if user == ctx.author:
107
            return await ctx.send(
108
                f"{user.mention} You can't {operation_t} yourself", delete_after=5.0)
109
        if operation_t == "kick":
110
            await user.kick(reason=reason)
111
        elif operation_t == "ban":
112
            await user.ban(reason=reason)
113
        elif operation_t == "softban":
114
            await user.ban(reason=reason)
115
            await user.unban(reason=reason)
116
        reason_str = f"\nReason: {reason}" if reason else ""
117
118
        await ctx.send(f"{user} was {operation_t}ed{reason_str}", delete_after=10.0)
119
        await user.send(f"You were {operation_t}ed from {ctx.guild.name}{reason_str}")
120
121
    # Kick
122
    @cog_ext.cog_slash(name="kick", guild_ids=cogbase.GUILD_IDS,
123
                       description="Kicks member from the server",
124
                       default_permission=False,
125
                       permissions=cogbase.PERMISSION_MODS)
126
    async def kick(self, ctx, user: discord.Member, *, reason=None):
127
        operation_t = "kick"
128
        await self.operation(ctx, user, operation_t, reason)
129
130
    # Ban
131
    @cog_ext.cog_slash(name="ban", guild_ids=cogbase.GUILD_IDS,
132
                       description="Ban member from the server",
133
                       default_permission=False,
134
                       permissions=cogbase.PERMISSION_ADMINS)
135
    async def ban(self, ctx, user: discord.Member, *, reason=None):
136
        operation_t = "ban"
137
        await self.operation(ctx, user, operation_t, reason)
138
139
    # Softban
140
    @cog_ext.cog_slash(name="softban", guild_ids=cogbase.GUILD_IDS,
141
                       description="Ban and unban the user, so their messages are deleted",
142
                       default_permission=False,
143
                       permissions=cogbase.PERMISSION_MODS)
144
    async def softban(self, ctx, user: discord.Member, *, reason=None):
145
        operation_t = "softban"
146
        await self.operation(ctx, user, operation_t, reason)
147
148
    # OTHER
149
    # Slow mode
150
    @cog_ext.cog_slash(name="slowmode", guild_ids=cogbase.GUILD_IDS,
151
                       description="Enable slowmode on current channel",
152
                       default_permission=False,
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 == 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 == 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