Failed Conditions
Branch BonHowi (f075ef)
by Bartosz
01:21
created

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

Complexity

Conditions 6

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 16
rs 8.6666
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="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
        await ctx.send(f"Clearing {number_to_delete} messages!", delete_after=3)
30
        async for message in ctx.channel.history(limit=number_to_delete + 1):
31
            messages.append(message)
32
        await ctx.channel.delete_messages(messages)
33
34
        await asyncio.sleep(5)
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}")
100
        await asyncio.sleep(duration)
101
        await user.remove_roles(muted)
102
        await ctx.send(f"{user.mention}'s mute is over", delete_after=30)
103
        await self.bot.send_message(user, "Your mute is over")
104
105
    # KICK FUNCTIONS
106
    @staticmethod
107
    async def operation(ctx, user: discord.Member, operation_t: str, reason=None):
108
        if user == ctx.author:
109
            return await ctx.send(
110
                f"{user.mention} You can't {operation_t} yourself", delete_after=5.0)
111
        if operation_t == "kick":
112
            await user.kick(reason=reason)
113
        elif operation_t == "ban":
114
            await user.ban(reason=reason)
115
        elif operation_t == "softban":
116
            await user.ban(reason=reason)
117
            await user.unban(reason=reason)
118
        reason_str = f"\nReason: {reason}" if reason else ""
119
120
        await ctx.send(f"{user} was {operation_t}ed{reason_str}")
121
        await user.send(f"You were {operation_t}ed from {ctx.guild.name}{reason_str}")
122
123
    # Kick
124
    @cog_ext.cog_slash(name="kick", guild_ids=cogbase.GUILD_IDS,
125
                       description="Kicks member from the server",
126
                       default_permission=False,
127
                       permissions=cogbase.PERMISSION_MODS)
128
    async def kick(self, ctx, user: discord.Member, *, reason=None):
129
        operation_t = "kick"
130
        await self.operation(ctx, user, operation_t, reason)
131
132
    # Ban
133
    @cog_ext.cog_slash(name="ban", guild_ids=cogbase.GUILD_IDS,
134
                       description="Ban member from the server",
135
                       default_permission=False,
136
                       permissions=cogbase.PERMISSION_ADMINS)
137
    async def ban(self, ctx, user: discord.Member, *, reason=None):
138
        operation_t = "ban"
139
        await self.operation(ctx, user, operation_t, reason)
140
141
    # Softban
142
    @cog_ext.cog_slash(name="softban", guild_ids=cogbase.GUILD_IDS,
143
                       description="Ban and unban the user, so their messages are deleted",
144
                       default_permission=False,
145
                       permissions=cogbase.PERMISSION_MODS)
146
    async def softban(self, ctx, user: discord.Member, *, reason=None):
147
        operation_t = "softban"
148
        await self.operation(ctx, user, operation_t, reason)
149
150
    # OTHER
151
    # Slow mode
152
    @cog_ext.cog_slash(name="slowmode", guild_ids=cogbase.GUILD_IDS,
153
                       description="Enable slowmode on current channel",
154
                       default_permission=False,
155
                       permissions=cogbase.PERMISSION_MODS)
156
    async def slowmode(self, ctx, seconds: int = 0):
157
        if seconds > 120:
158
            return await ctx.send(":no_entry: Amount can't be over 120 seconds")
159
        if seconds == 0:
160
            await ctx.channel.edit(slowmode_delay=seconds)
161
            a = await ctx.send("Slowmode is off for this channel")
162
            await a.add_reaction("a:redcard:871861842639716472")
163
        else:
164
            if seconds == 1:
165
                numofsecs = "second"
166
            else:
167
                numofsecs = "seconds"
168
            await ctx.channel.edit(slowmode_delay=seconds)
169
            confirm = await ctx.send(
170
                f"{ctx.author.display_name} set the channel slow mode delay to `{seconds}` {numofsecs}\n"
171
                f"To turn this off use /slowmode")
172
            await confirm.add_reaction("a:ResidentWitcher:871872130021736519")
173
174
175
def setup(bot: commands.Bot):
176
    bot.add_cog(AdminCog(bot))
177