-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c6ad4a
commit d93b481
Showing
1 changed file
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import { | ||
CollectorFilter, | ||
DMChannel, | ||
Message, | ||
MessageReaction, | ||
User, | ||
} from "discord.js"; | ||
import state from "../../common/state"; | ||
import { | ||
Command, | ||
CommandHandler, | ||
DiscordEvent, | ||
EventLocation, | ||
} from "../../event-distribution"; | ||
import { getMember, textLog } from "../../common/moderator"; | ||
import { createYesBotLogger } from "../../log"; | ||
|
||
const logger = createYesBotLogger("programs", "DmMenu"); | ||
|
||
const removeIgnore = (channel: DMChannel) => { | ||
const index = state.ignoredGroupDMs.indexOf(channel.id); | ||
if (index > -1) { | ||
state.ignoredGroupDMs.splice(index, 1); | ||
} | ||
}; | ||
|
||
const availableEmojiOptions = ["👶", "🦥"]; | ||
|
||
@Command({ | ||
event: DiscordEvent.MESSAGE, | ||
trigger: "!menu", | ||
location: EventLocation.DIRECT_MESSAGE, | ||
description: "This handler is for the DM menu", | ||
}) | ||
class ShowMenu implements CommandHandler<DiscordEvent.MESSAGE> { | ||
async handle(message: Message): Promise<void> { | ||
const member = getMember(message.author.id); | ||
const dmChannel = message.channel as DMChannel; | ||
|
||
if (!member) { | ||
await dmChannel.send( | ||
"Hey, I am the bot of the Yes Theory Fam Discord Server :) Looks like you are not on it currently, so I cannot really do a lot for you. If you'd like to join, click here: https://discord.gg/yestheory" | ||
); | ||
return; | ||
} | ||
|
||
if (state.ignoredGroupDMs.includes(dmChannel.id)) return; | ||
|
||
const optionsMessage = await message.reply( | ||
"Hey, I'm just a bot! Most of what I can do, I do on the YesFam discord, so talk to me there instead! I can help you change your name, though, if you're new around here. Click the :baby: if you want to change your name! Or if you feel like you need a break you can click on the :sloth:" | ||
); | ||
|
||
availableEmojiOptions.forEach( | ||
async (emoji) => await optionsMessage.react(emoji) | ||
); | ||
|
||
try { | ||
const reactions = await emojiCollector(optionsMessage); | ||
|
||
switch (reactions.emoji.toString()) { | ||
case "👶": | ||
await nameCollector(dmChannel, message); | ||
} | ||
} catch (err) { | ||
await handleError(optionsMessage, dmChannel); | ||
} | ||
|
||
await optionsMessage.delete(); | ||
} | ||
} | ||
|
||
const proposeNameChange = async (name: string, botMessage: Message) => { | ||
await botMessage.reply( | ||
"Perfect! I've sent your name request to the mods, hopefully they answer soon! In the meantime, you're free to roam around the server and explore. Maybe post an introduction to get started? :grin:" | ||
); | ||
const message = `Username: ${botMessage.author.toString()} would like to rename to "${name}". Allow?`; | ||
try { | ||
const sentMessage = await textLog(message); | ||
sentMessage.react("✅").then(() => sentMessage.react("🚫")); | ||
sentMessage | ||
.awaitReactions({ | ||
filter: (_, user: User) => { | ||
return !user.bot; | ||
}, | ||
max: 1, | ||
time: 6000000, | ||
errors: ["time"], | ||
}) | ||
.then((collected) => { | ||
const reaction = collected.first(); | ||
switch (reaction.emoji.toString()) { | ||
case "✅": | ||
const member = getMember(botMessage.author.id); | ||
member.setNickname(name); | ||
sentMessage.delete(); | ||
textLog(`${botMessage.author.toString()} was renamed to ${name}.`); | ||
break; | ||
case "🚫": | ||
sentMessage.delete(); | ||
textLog( | ||
`${botMessage.author.toString()} was *not* renamed to ${name}.` | ||
); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
}); | ||
} catch (err) { | ||
logger.error("(proposeNameChange) Error changing name: ", err); | ||
} | ||
}; | ||
|
||
const nameCollector = async (dmChannel: DMChannel, message: Message) => { | ||
const requestMessage = await dmChannel.send( | ||
"Okay, what's your name then? Please only respond with your name like Henry or Julie, that makes things easier for the Supports! :upside_down:" | ||
); | ||
state.ignoredGroupDMs.push(dmChannel.id); | ||
const nameMessage = await dmChannel.awaitMessages({ | ||
filter: (message) => !message.author.bot, | ||
time: 60000, | ||
max: 1, | ||
}); | ||
removeIgnore(dmChannel); | ||
|
||
if (nameMessage.size === 0) { | ||
requestMessage.delete(); | ||
throw "No response"; | ||
} | ||
|
||
const requestedName = nameMessage.first().content; | ||
proposeNameChange(requestedName, message); | ||
await requestMessage.delete(); | ||
}; | ||
|
||
const emojiCollector = async (optionsMessage: Message) => { | ||
const filter: CollectorFilter<[MessageReaction, User]> = (reaction, user) => | ||
availableEmojiOptions.includes(reaction.emoji.name) && !user.bot; | ||
|
||
const reactions = await optionsMessage.awaitReactions({ | ||
filter, | ||
time: 60000, | ||
max: 1, | ||
}); | ||
if (reactions.size === 0) throw "No reactions"; | ||
|
||
return reactions.first(); | ||
}; | ||
|
||
const handleError = async (optionsMessage: Message, dmChannel: DMChannel) => { | ||
removeIgnore(dmChannel); | ||
|
||
dmChannel.send( | ||
"Because of technical reasons I can only wait 60 seconds for a reaction. I removed the other message to not confuse you. If you need anything from me, just drop me a message!" | ||
); | ||
|
||
await optionsMessage.delete(); | ||
}; |