generated from jphacks/JP_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from jphacks/Discordbot
Discordbot作りました
- Loading branch information
Showing
19 changed files
with
3,180 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 @@ | ||
.env |
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,3 @@ | ||
{ | ||
"recommendations": ["denoland.vscode-deno"] | ||
} |
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,24 @@ | ||
{ | ||
"deno.enablePaths": [ | ||
"supabase/functions" | ||
], | ||
"deno.lint": true, | ||
"deno.unstable": [ | ||
"bare-node-builtins", | ||
"byonm", | ||
"sloppy-imports", | ||
"unsafe-proto", | ||
"webgpu", | ||
"broadcast-channel", | ||
"worker-options", | ||
"cron", | ||
"kv", | ||
"ffi", | ||
"fs", | ||
"http", | ||
"net" | ||
], | ||
"[typescript]": { | ||
"editor.defaultFormatter": "denoland.vscode-deno" | ||
} | ||
} |
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,5 @@ | ||
現在は、package.json内のscriptsのstartとdebugの読み取るファイルを変更しながら実行している。 | ||
Discord botに現在権限をすべて渡している状態のため注意(デメリットがあるのかはわからない) | ||
リアクション機能は、message.react('🤔')のように宣言するか、 | ||
import { get } from 'node-emoji'を使い、const emoji = get(':smile:') ?? ''でemojiにリアクションを入れて宣言するかの2択 | ||
ranking.tsのやつはランキング機能を仮に作ってみたもので、未完成です。 |
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,19 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const discord_js_1 = require("discord.js"); | ||
require("dotenv/config"); | ||
const node_emoji_1 = require("node-emoji"); | ||
const client = new discord_js_1.Client({ | ||
intents: [discord_js_1.GatewayIntentBits.Guilds, discord_js_1.GatewayIntentBits.GuildMessages, discord_js_1.GatewayIntentBits.MessageContent] | ||
}); | ||
client.once('ready', () => { | ||
console.log(`Logged in as ${client.user?.tag}`); | ||
}); | ||
client.on('messageCreate', (message) => { | ||
if (message.author.bot) | ||
return; | ||
const emoji = (0, node_emoji_1.get)(':smile:') ?? ''; | ||
message.react('🤔'); | ||
message.react(emoji); | ||
}); | ||
client.login(process.env.DISCORD_TOKEN); |
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,80 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const discord_js_1 = require("discord.js"); | ||
const dotenv_1 = __importDefault(require("dotenv")); | ||
dotenv_1.default.config(); | ||
const client = new discord_js_1.Client({ | ||
intents: [discord_js_1.GatewayIntentBits.Guilds, discord_js_1.GatewayIntentBits.GuildMessages, discord_js_1.GatewayIntentBits.GuildMessageReactions], | ||
partials: [discord_js_1.Partials.Message, discord_js_1.Partials.Channel, discord_js_1.Partials.Reaction], | ||
}); | ||
const reactionCounts = new Map(); | ||
const commands = [ | ||
new discord_js_1.SlashCommandBuilder() | ||
.setName('ranking') | ||
.setDescription('Displays the reaction ranking.'), | ||
]; | ||
const rest = new discord_js_1.REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN); | ||
(async () => { | ||
try { | ||
console.log('Started refreshing application (/) commands.'); | ||
await rest.put(discord_js_1.Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID), { body: commands.map(command => command.toJSON()) }); | ||
console.log('Successfully reloaded application (/) commands.'); | ||
} | ||
catch (error) { | ||
console.error(error); | ||
} | ||
})(); | ||
client.once('ready', () => { | ||
console.log(`Logged in as ${client.user?.tag}`); | ||
}); | ||
client.on(discord_js_1.Events.MessageReactionAdd, async (reaction, user) => { | ||
if (reaction.partial) { | ||
await reaction.fetch(); | ||
} | ||
const userId = user.id; | ||
const emoji = (reaction.emoji.name || reaction.emoji.id || 'unknown'); | ||
if (!reactionCounts.has(userId)) { | ||
reactionCounts.set(userId, new Map()); | ||
} | ||
const userReactions = reactionCounts.get(userId); | ||
userReactions.set(emoji, (userReactions.get(emoji) || 0) + 1); | ||
}); | ||
client.on(discord_js_1.Events.MessageReactionRemove, async (reaction, user) => { | ||
if (reaction.partial) { | ||
await reaction.fetch(); | ||
} | ||
const userId = user.id; | ||
const emoji = (reaction.emoji.name || reaction.emoji.id || 'unknown'); | ||
const userReactions = reactionCounts.get(userId); | ||
if (userReactions) { | ||
const newCount = (userReactions.get(emoji) || 1) - 1; | ||
if (newCount > 0) { | ||
userReactions.set(emoji, newCount); | ||
} | ||
else { | ||
userReactions.delete(emoji); | ||
} | ||
} | ||
}); | ||
client.on(discord_js_1.Events.InteractionCreate, async (interaction) => { | ||
if (!interaction.isCommand()) | ||
return; | ||
if (interaction.commandName === 'ranking') { | ||
const sortedUsers = [...reactionCounts.entries()] | ||
.map(([userId, reactions]) => ({ | ||
userId, | ||
count: [...reactions.values()].reduce((acc, val) => acc + val, 0) | ||
})) | ||
.sort((a, b) => b.count - a.count) | ||
.slice(0, 3); | ||
const rankingMessage = sortedUsers.map((user, index) => { | ||
const member = interaction.guild?.members.cache.get(user.userId); | ||
return `${index + 1}. ${member?.user.tag || 'Unknown User'} - ${user.count} reactions`; | ||
}).join('\n') || 'No reactions to display.'; | ||
await interaction.reply({ content: `**Reaction Ranking:**\n${rankingMessage}`, ephemeral: true }); | ||
} | ||
}); | ||
client.login(process.env.DISCORD_TOKEN); |
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,68 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const discord_js_1 = require("discord.js"); | ||
const dotenv_1 = __importDefault(require("dotenv")); | ||
dotenv_1.default.config(); | ||
const client = new discord_js_1.Client({ intents: [discord_js_1.GatewayIntentBits.Guilds, discord_js_1.GatewayIntentBits.GuildMessages, discord_js_1.GatewayIntentBits.MessageContent] }); | ||
const mentionAuthorMap = new Map(); | ||
client.once('ready', () => { | ||
console.log(`Logged in as ${client.user?.tag}`); | ||
}); | ||
client.on('messageCreate', async (message) => { | ||
if (message.author.bot) | ||
return; | ||
const mentionedUser = message.mentions.users.first(); | ||
const mentionAuthor = message.author; | ||
if (mentionedUser) { | ||
mentionAuthorMap.set(message.id, mentionAuthor.id); | ||
if (message.channel instanceof discord_js_1.TextChannel) { | ||
try { | ||
const messageLink = `https://discord.com/channels/${message.guild?.id}/${message.channel.id}/${message.id}`; | ||
await mentionAuthor.send('まだメッセージを確認していません。メンションされたユーザーが確認するとお知らせします。'); | ||
const buttonLink = new discord_js_1.ButtonBuilder() | ||
.setLabel(`メッセージを確認`) | ||
.setStyle(discord_js_1.ButtonStyle.Link) | ||
.setURL(messageLink); | ||
const buttonConfirm = new discord_js_1.ButtonBuilder() | ||
.setCustomId(mentionAuthor.id) | ||
.setLabel('確認しました!') | ||
.setStyle(discord_js_1.ButtonStyle.Primary) | ||
.setEmoji('👍'); | ||
const row = new discord_js_1.ActionRowBuilder().addComponents(buttonLink, buttonConfirm); | ||
try { | ||
await mentionedUser.send({ | ||
content: `${mentionedUser.username}さん、@ ${mentionAuthor.username}からメッセージが届いています。メッセージを確認してください!`, | ||
components: [row], | ||
}); | ||
} | ||
catch (error) { | ||
console.error('Failed to send message to the mentioned user:', error); | ||
} | ||
} | ||
catch (error) { | ||
console.error('Error creating button:', error); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
client.on('interactionCreate', async (interaction) => { | ||
if (!interaction.isButton()) | ||
return; | ||
// if (interaction.customId === 'confirm') { | ||
interaction.message?.mentions.users.first(); | ||
try { | ||
await interaction.reply({ content: '確認ありがとう!', ephemeral: true }); | ||
} | ||
catch (error) { | ||
console.error('Failed to send confirmation to the mention author:', error); | ||
} | ||
// } | ||
const mentionAuthor = await client.users.fetch(interaction.customId); | ||
await mentionAuthor.send('メンションしたユーザーがメッセージを確認しました!'); | ||
await interaction.message.delete(); | ||
}); | ||
client.login(process.env.DISCORD_TOKEN); |
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,42 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const discord_js_1 = require("discord.js"); | ||
const dotenv_1 = __importDefault(require("dotenv")); | ||
dotenv_1.default.config(); | ||
const client = new discord_js_1.Client({ | ||
intents: [discord_js_1.GatewayIntentBits.Guilds, discord_js_1.GatewayIntentBits.GuildMessages, discord_js_1.GatewayIntentBits.GuildMessageReactions], | ||
partials: [discord_js_1.Partials.Message, discord_js_1.Partials.Channel, discord_js_1.Partials.Reaction], | ||
}); | ||
client.once('ready', () => { | ||
console.log(`Logged in as ${client.user?.tag}`); | ||
}); | ||
client.on(discord_js_1.Events.MessageReactionAdd, async (reaction, user) => { | ||
if (reaction.partial) { | ||
try { | ||
await reaction.fetch(); | ||
} | ||
catch (error) { | ||
console.error('Something went wrong when fetching the message:', error); | ||
return; | ||
} | ||
} | ||
console.log(` ${user.tag}がリアクションを追加しました!`); | ||
console.log(`Emoji: ${reaction.emoji.name}`); | ||
}); | ||
client.on(discord_js_1.Events.MessageReactionRemove, async (reaction, user) => { | ||
if (reaction.partial) { | ||
try { | ||
await reaction.fetch(); | ||
} | ||
catch (error) { | ||
console.error('Something went wrong when fetching the message:', error); | ||
return; | ||
} | ||
} | ||
console.log(`${user.tag}がリアクションを削除しました!`); | ||
console.log(`Emoji: ${reaction.emoji.name}`); | ||
}); | ||
client.login(process.env.DISCORD_TOKEN); |
Oops, something went wrong.