summaryrefslogtreecommitdiff
path: root/cogs/Macro.py
blob: cef67f362c88176c366585d9aa01b001d9e0737a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from discord.ext import commands
import discord
import pickle
import os
import MySQLdb
import configparser
from discord.ext.commands.cooldowns import BucketType
import time
import datetime
colours = { "default": 0,
           "teal": 0x1abc9c,
           "dark teal": 0x11806a,
           "green": 0x2ecc71,
           "dark green": 0x1f8b4c,
           "blue": 0x3498db,
           "dark blue": 0x206694,
           "purple": 0x9b59b6,
           "dark purple": 0x71368a,
           "magenta": 0xe91e63,
           "dark magenta": 0xad1457,
           "gold": 0xf1c40f,
           "dark gold": 0xc27c0e,
           "orange": 0xe67e22,
           "dark orange": 0xa84300,
           "red": 0xe74c3c,
           "dark red": 0x992d22,
           "lighter grey": 0x95a5a6,
           "dark grey": 0x607d8b,
           "light grey": 0x979c9f,
           "darker grey": 0x546e7a,
           "blurple": 0x7289da,
           "greyple": 0x99aab5}


class Macro(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        global cooldown_dict
        cooldown_dict = {}

    def blacklist_check(ctx):
        # Read config
        config = configparser.ConfigParser()
        config.read('config.ini')

        # Create database link
        db = MySQLdb.connect(host=config["mysqldb"]["host"], user=config["mysqldb"]["user"],
                             passwd=config["mysqldb"]["passwd"], db=config["mysqldb"]["db"])
        c = db.cursor()
        c.execute("SELECT `user_id` from `blacklist` WHERE `user_id` = %s" % str(ctx.author.id))
        result = c.fetchone()

        if result is None:
            return True
        else:
            return

    @staticmethod
    async def cooldown_check(ctx):
        global cooldown_dict
        # if cooldown_dict is None:
        d = datetime.datetime.now()
        unixtime = time.mktime(d.timetuple())
        cooldown_dict[ctx.author.id] = unixtime
        #await ctx.channel.send(content=int(unixtime))

        return True
        # if result is None:
        #     return True
        # else:
        #     return

    def bot_admin_check(ctx):
        # Read config
        config = configparser.ConfigParser()
        config.read('config.ini')

        results = list(map(int, config["conf"]["bot_admins"].split(",")))

        return ctx.message.author.id in list(results)

    @commands.check(blacklist_check)
    @commands.group(invoke_without_command=True)
    async def macro(self, ctx):
        """WIP."""
        prefix = await self.bot.get_prefix(ctx.message)
        prefix.remove(f"<@{self.bot.user.id}> ")
        prefix.remove(f"<@!{self.bot.user.id}> ")
        prefix = prefix[0]
        message_embed = discord.Embed(title=f"Commands for `{prefix}macro`",
                                      description="WIP",
                                      colour=colours["blue"])
        message_embed.add_field(name=f"{prefix}macro add [trigger word] [content]",
                                value="Adds a new macro for you to use.",
                                inline=False)
        message_embed.add_field(name=f"{prefix}macro list",
                                value="Get a list of your current macros", inline=False)
        message_embed.add_field(name=f"{prefix}macro remove [trigger word]",
                                value="Removes a macro from current macros.", inline=False)
        await ctx.send(embed=message_embed)
        True

    @commands.check(blacklist_check)
    @macro.command()
    async def add(self, ctx, *arg):
        arg = list(arg)

        trigger_word = arg.pop(0)
        trigger_phrase = " ".join(arg)

        try:
            user_macros = await self.load_obj(ctx.author.id)
        except EOFError:
            user_macros = {}

        if user_macros is None:
            user_macros = {trigger_word, trigger_phrase}
        else:
            user_macros = dict(user_macros)
            user_macros[trigger_word] = trigger_phrase

        await self.save_obj(user_macros, ctx.author.id)
        await ctx.send(content=f"({trigger_word})({trigger_phrase})")

    @commands.check(blacklist_check)
    @macro.command()
    async def remove(self, ctx, arg):
        arg = str(arg)

        try:
            user_macros = await self.load_obj(ctx.author.id)
        except EOFError:
            user_macros = {}

        reslt = user_macros.pop(arg, "0")
        if reslt is None:
            await ctx.send(content=f"No macro with the name {arg} exists.")
        else:
            await self.save_obj(user_macros, ctx.author.id)
            await ctx.send(content=f"Removed {reslt}")

        # if arg in user_macros:
        #     user_macros.remove(arg)
        #     await ctx.send(content=str(user_macros))
        # else:
        #     await ctx.send(content="No: " + str(user_macros))

    @commands.check(blacklist_check)
    @macro.command()
    async def list(self, ctx):
        user_dict = await self.load_obj(ctx.author.id)

        msg = ""
        offset = 1

        for item in user_dict:
            msg = f"{msg}\n{item}) {user_dict[item]}"

        embed_msg = discord.Embed(title="List of Macros",
                                  description=msg,
                                  colour=colours["blue"])

        await ctx.send(embed=embed_msg)

    # @commands.cooldown(1, 60.0, BucketType.user)
    @commands.check(blacklist_check)
    @commands.Cog.listener()
    async def on_message(self, ctx):
        if await self.cooldown_check(ctx):
            try:
                user_macros = await self.load_obj(ctx.author.id)
            except EOFError:
                user_macros = {}

            for macro in user_macros:
                if macro == ctx.content:
                    await ctx.channel.send(content=user_macros[macro])

    @commands.check(bot_admin_check)
    @macro.command()
    async def blacklist(self, ctx, user: discord.Member):
        # Read config
        config = configparser.ConfigParser()
        config.read('config.ini')

        # Create database link
        db = MySQLdb.connect(host=config["mysqldb"]["host"], user=config["mysqldb"]["user"],
                             passwd=config["mysqldb"]["passwd"], db=config["mysqldb"]["db"])
        c = db.cursor()
        c.execute("INSERT INTO `blacklist` (`user_id`, `reason`) VALUES (%s,'%s');" % (user.id, ""))
        db.commit()

        message_embed = discord.Embed(title=f"Blacklisted {user}",
                                      description="",
                                      colour=colours["blue"])
        await ctx.send(embed=message_embed)

    @staticmethod
    async def save_obj(obj, filename: str):
        with open(f"./macros/{filename}.pkl", 'wb') as f:
            pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)

    @staticmethod
    async def load_obj(owner_id):
        if not os.path.isfile(f"./macros/{owner_id}.pkl"):
            os.mknod(f"./macros/{owner_id}.pkl")

        with open(f"./macros/{owner_id}.pkl", "rb") as f:
            return pickle.load(f)


def setup(bot):
    print("INFO: Loading [Macro]... ", end="")
    bot.add_cog(Macro(bot))
    print("Done!")


def teardown(bot):
    print("INFO: Unloading [Macro]")