summaryrefslogtreecommitdiff
path: root/cogs/Define.py
blob: 0b3b1f540cf96776cbb32256b451bc8440f74812 (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
import os

from discord.ext import commands
import discord
import requests
import json
import importlib
import utils
importlib.reload(utils)


class Define(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        global extension_name
        extension_name = "[Define] "

    @commands.command(
        aliases=[],
        application_command_meta=commands.ApplicationCommandMeta(
            options=[
                discord.ApplicationCommandOption(
                    name="word",
                    description="The word you want a definition for.",
                    type=discord.ApplicationCommandOptionType.string,
                    required=True,
                )
            ],
        )
    )
    @commands.bot_has_permissions(embed_links=True)
    async def define(self, ctx, word):
        """Gives you definitions of a word according to the Oxford English Dictionary."""
        app_id = os.environ.get('oxford_app_id')
        app_key = os.environ.get('oxford_app_key')
        language = "en-gb"

        url_define = f"https://od-api.oxforddictionaries.com:443/api/v2/entries/{language}/{word.lower()}"
        result_define = requests.get(url_define, headers={"app_id": app_id, "app_key": app_key})
        print(result_define.text)
        data_define = json.loads(result_define.text)

        def_dict = {}
        if "error" in data_define:
            # Create embedded message
            emb = await utils.embed(ctx, f"Definitions for `{word}`", "No definitions found.")
            await ctx.send(embed=emb)

        else:
            for lexicalEntries in data_define["results"][0]["lexicalEntries"]:
                category = lexicalEntries["lexicalCategory"]["text"]
                def_dict[category] = ""

                # Added this check as some words appear to have 'subsenses' instead of 'senses'. Fuckers.
                # print(f"{lexicalEntries['entries'][0]}")
                if 'definitions' in lexicalEntries['entries'][0]['senses'][0]:
                    for i, definition in enumerate(lexicalEntries['entries'][0]['senses'], start=1):
                        def_dict[category] = f"{def_dict[category]}**{i})** {definition['definitions'][0]}\n"
                # elif 'subsenses' in lexicalEntries['entries'][0]['senses'][0]:
                #     for i, definition in enumerate(lexicalEntries['entries'][0]['senses'][0]['subsenses'], start=1):
                #         def_dict[category] = f"{def_dict[category]}**{i})** {definition['definitions'][0]}\n"
                else:
                    for i, definition in enumerate(lexicalEntries['entries'][0]['senses'][0]['crossReferenceMarkers'], start=1):
                        def_dict[category] = f"{def_dict[category]}**{i})** {definition}\n"

            # Create embedded message
            emb = await utils.embed(ctx, f"Definitions for `{word}`", "")
            for item in def_dict:
                emb = await utils.field(emb, item, def_dict[item])
            await ctx.send(embed=emb)

    @commands.command(
        aliases=[],
        application_command_meta=commands.ApplicationCommandMeta(
            options=[
                discord.ApplicationCommandOption(
                    name="word",
                    description="The word you want a definition for.",
                    type=discord.ApplicationCommandOptionType.string,
                    required=True,
                )
            ],
        )
    )
    @commands.bot_has_permissions(embed_links=True)
    async def urban(self, ctx, word):
        """Gives you definitions of a word according to the Urban Dictionary."""
        url_urban = f"http://api.urbandictionary.com/v0/define?term={word.lower()}"
        result_urban = requests.get(url_urban)
        data_urban = json.loads(result_urban.text)
        str_def = ""

        for i, item in enumerate(data_urban["list"]):
            char_trail = "..."
            char_limit = 187 - len(char_trail)

            if len(item['definition']) > char_limit:
                item_def = f"{item['definition'][:char_limit]}..."
            else:
                item_def = f"{item['definition']}"

            str_definition = item_def.replace('\r', '').replace('\n', ' ')
            str_def = str_def + f"**{i+1})** {str_definition}\n"

        emb = await utils.embed(ctx, f"Urban definitions for `{word}`", str_def)
        await ctx.send(embed=emb)


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


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