-
Notifications
You must be signed in to change notification settings - Fork 10
/
bot_emoji.lua
138 lines (121 loc) · 3.32 KB
/
bot_emoji.lua
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
-- Copyright (C) 2018 Jérôme Leclercq
-- This file is part of the "Not a Bot" application
-- For conditions of distribution and use, see copyright notice in LICENSE
local emojiTable = require("./data_emoji.lua")
local emojiByName = {}
for _, emojiData in pairs(emojiTable) do
for _, name in pairs(emojiData.names) do
emojiByName[name] = emojiData
end
end
Bot.EmojiByName = emojiByName
local emojiByCode = {}
for _, emojiData in pairs(emojiTable) do
for _, code in pairs(emojiData.codes) do
emojiByCode[code] = emojiData
end
end
Bot.EmojiByCode = emojiByCode
local emojiGlobalCache = {}
local emojiGuildsCache = {}
local emojiGlobalGuildCache = {}
function Bot:GetEmojiData(guild, emojiIdOrName)
if (not guild) then
assert(emojiIdOrName:match("^%d+$"), "When searching in global emoji cache, id must be used")
end
local emojiData
-- Check in global cache first
emojiData = emojiGlobalCache[emojiIdOrName]
if (emojiData) then
return emojiData
end
-- Not in global cache, search in guild cache
if (guild) then
local emojiGuildCache = emojiGuildsCache[guild.id]
if (emojiGuildCache) then
emojiData = emojiGuildCache[emojiIdOrName]
else
emojiGuildCache = {}
emojiGuildsCache[guild.id] = emojiGuildCache
end
else
emojiData = emojiGlobalGuildCache[emojiIdOrName]
end
if (emojiData) then
return emojiData
end
-- First check if it is a Discord emoji
local discordEmoji = emojiByCode[emojiIdOrName]
if (not discordEmoji) then
discordEmoji = emojiByName[emojiIdOrName]
end
if (discordEmoji) then
emojiData = {}
emojiData.Custom = false
emojiData.Id = discordEmoji.codes[1]
emojiData.Name = discordEmoji.names[1]
emojiData.MentionString = discordEmoji.codes[1]
else
-- Not a discord emoji, check in guild
if (guild) then
for _,emoji in pairs(guild.emojis) do
if (emojiIdOrName == emoji.id or emojiIdOrName == emoji.name) then
emojiData = {}
emojiData.Custom = true
emojiData.Emoji = emoji
emojiData.Id = emoji.id
emojiData.Name = emoji.name
emojiData.MentionString = emoji.mentionString
emojiData.FromGuild = guild
break
end
end
else
for _, guild in pairs(Bot.Client.guilds) do
for _,emoji in pairs(guild.emojis) do
if (emojiIdOrName == emoji.id) then
emojiData = {}
emojiData.Custom = true
emojiData.Emoji = emoji
emojiData.Id = emoji.id
emojiData.Name = emoji.name
emojiData.MentionString = emoji.mentionString
emojiData.FromGuild = guild
break
end
end
if (emojiData) then
break
end
end
end
end
if (not emojiData) then
-- Not a valid emoji
return nil
end
-- Register new emoji
if (emojiData.Custom) then
if (guild) then
local emojiGuildCache = emojiGuildsCache[guild.id]
emojiGuildCache[emojiData.Id] = emojiData
emojiGuildCache[emojiData.Name] = emojiData
else
emojiGlobalGuildCache[emojiData.Id] = emojiData
end
else
emojiGlobalCache[emojiData.Id] = emojiData
emojiGlobalCache[emojiData.Name] = emojiData
end
return emojiData
end
local function deleteGuildCache(guild)
emojiGuildsCache[guild.id] = nil
for k, emojiData in pairs(emojiGlobalGuildCache) do
if (emojiData.FromGuild == guild) then
emojiGlobalGuildCache[k] = nil
end
end
end
Bot.Client:on('emojisUpdate', deleteGuildCache)
Bot.Client:on("guildDelete", deleteGuildCache)