Created
March 11, 2020 14:49
-
-
Save yhay81/cd6d771fbcc7aadc0ff0bb5fa56f8ebe to your computer and use it in GitHub Desktop.
mattari admin
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
import Discord from "discord.js"; | |
const client = new Discord.Client(); | |
// ready | |
/* Emitted when the client becomes ready to start working. */ | |
client.on("ready", function() { | |
console.log(`the client becomes ready to start`); | |
console.log(`I am ready! Logged in as ${client.user.tag}!`); | |
console.log( | |
`Bot has started, with ${client.users} users, in ${client.channels} channels of ${client.guilds} guilds.` | |
); | |
client.user.setActivity("まったり個人Web開発"); | |
}); | |
const alphabet = [ | |
"🇦", | |
"🇧", | |
"🇨", | |
"🇩", | |
"🇪", | |
"🇫", | |
"🇬", | |
"🇭", | |
"🇮", | |
"🇯", | |
"🇰", | |
"🇱", | |
"🇲", | |
"🇳", | |
"🇴", | |
"🇵", | |
"🇶", | |
"🇷", | |
"🇸", | |
"🇹", | |
"🇺", | |
"🇻", | |
"🇼", | |
"🇽", | |
"🇾", | |
"🇿" | |
]; | |
let messageId; | |
let roleDictionary: Map<string, Discord.Role>; | |
// message | |
/* Emitted whenever a message is created. | |
PARAMETER TYPE DESCRIPTION | |
message Message The created message */ | |
client.on("message", async message => { | |
if ( | |
!( | |
message.content === "!プロジェクト一覧" && | |
message.author.id === "432354940367929344" | |
) | |
) | |
return; | |
const roles = Array.from( | |
message.guild.roles.cache.filter( | |
role => role.name.startsWith("P:") && role.name !== "プロジェクトなしさん" | |
) | |
); | |
roleDictionary = new Map<string, Discord.Role>(); | |
roles.forEach((role, index) => { | |
roleDictionary.set(alphabet[index], role[1]); | |
}); | |
const embed = { | |
title: "プロジェクト一覧", | |
color: 0xf8e71c, | |
description: Array.from(roleDictionary) | |
.map(([emoji, role]) => `${emoji} ${role.name}`) | |
.join("\n") | |
}; | |
const botMessage = await message.channel.send({ embed }); | |
roleDictionary.forEach(async (role, emoji) => { | |
await botMessage.react(emoji); | |
}); | |
messageId = botMessage.id; | |
}); | |
// guildMemberAdd | |
/* Emitted whenever a user joins a guild. | |
PARAMETER TYPE DESCRIPTION | |
member GuildMember The member that has joined a guild */ | |
client.on("guildMemberAdd", member => { | |
console.log(`a user joins a guild: ${member.id}`); | |
member.roles.add("687256689631690753"); | |
}); | |
// messageReactionAdd | |
/* Emitted whenever a reaction is added to a message. | |
PARAMETER TYPE DESCRIPTION | |
messageReaction MessageReaction The reaction object | |
user User The user that applied the emoji or reaction emoji */ | |
client.on("messageReactionAdd", async (messageReaction, user: Discord.User) => { | |
if (messageReaction.message.id !== messageId) return; | |
const member = messageReaction.message.guild.member(user); | |
if (member.id === "687260287769772040") return; | |
console.log(`a reaction is added to a message`); | |
member.roles.add(roleDictionary.get(messageReaction.emoji.name)); | |
}); | |
// messageReactionRemove | |
/* Emitted whenever a reaction is removed from a message. | |
PARAMETER TYPE DESCRIPTION | |
messageReaction MessageReaction The reaction object | |
user User The user that removed the emoji or reaction emoji */ | |
client.on("messageReactionRemove", function( | |
messageReaction, | |
user: Discord.User | |
) { | |
if (messageReaction.message.id !== messageId) return; | |
const member = messageReaction.message.guild.member(user); | |
console.log(`a reaction is removed from a message`); | |
member.roles.remove(roleDictionary.get(messageReaction.emoji.name)); | |
}); | |
require("dotenv").config(); | |
client.login(process.env.DISCORD_BOT_TOKEN); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment