Skip to content

Commit

Permalink
Merge pull request #1 from tissueMO/feature/follow
Browse files Browse the repository at this point in the history
ユーザー追従機能追加
  • Loading branch information
tissueMO committed Jun 11, 2024
2 parents 508b508 + b02dc01 commit 55316e7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
14 changes: 14 additions & 0 deletions extensions/discord/config.example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module.exports = {
commands: [
{
guildId: '111111111111111111',
name: 'example',
description: 'Sample Command',
},
],
hooks: [
{
guildId: '111111111111111111',
Expand All @@ -19,4 +26,11 @@ module.exports = {
},
},
],
follows: [
{
guildId: '111111111111111111',
followerUserId: '111111111111111111',
followeeUserId: '111111111111111111',
},
],
};
37 changes: 35 additions & 2 deletions extensions/discord/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,59 @@ client.on('interactionCreate', async (interaction) => {
});

/**
* ミーティングデバイスの利用状態変更を検知してフック処理を行います
* ミーティングデバイスの利用状態変更を検知して任意のフック処理を行います
*/
client.on('voiceStateUpdate', async (oldState, newState) => {
const guildId = newState.guild.id;
const guildName = newState.guild.name;
const userId = newState.id;
const username = (await newState.guild.members.fetch(newState.id)).user.username;
const user = (await newState.guild.members.fetch(newState.id)).user;
const username = user.username;
const state = getChangedState(oldState, newState);

if (state) {
console.info(`[${guildName}]:[${username}] ミーティングデバイス ${state}`);

const results = await Promise.allSettled(
config.hooks
.filter(h => h.guildId === guildId && h.userId === userId && h.state === state)
.map(({ hook: { url, method, headers, data } }) => axios.request({ url, method, headers, data }))
);

console.info(`${results.length}件のフック処理が実行されました。(成功=${results.filter(r => r.status === 'fulfilled').length}, 失敗=${results.filter(r => r.status === 'rejected').length})`);
}
});

/**
* ボイスチャンネルの移動に連動して任意のユーザーに追従するフック処理を行います。
*/
client.on('voiceStateUpdate', async (oldState, newState) => {
const guildId = newState.guild.id;
const guildName = newState.guild.name;
const userId = newState.id;
const user = (await newState.guild.members.fetch(newState.id)).user;
const username = user.username;

const followers = await Promise.all(
config.follows
.filter(f => f.guildId === guildId && f.followeeUserId === userId)
.map(f => newState.guild.members.fetch(f.followerUserId))
);

if (followers.length && newState.channelId) {
console.info(`[${guildName}]:[${username}] 追従イベント`);

const channel = await newState.guild.channels.fetch(newState.channelId);
const results = await Promise.allSettled(
followers
.filter(f => !!f.voice.channelId && newState.channelId !== f.voice.channelId)
.map(f => f.voice.setChannel(channel))
);

console.info(`${results.length}件のフォロー処理が実行されました。(成功=${results.filter(r => r.status === 'fulfilled').length}, 失敗=${results.filter(r => r.status === 'rejected').length})`);
}
});

/**
* ミーティングデバイスの利用状態が変更された場合に変更後の使用状態文字列を返します。
* @param {VoiceState} oldState
Expand Down

0 comments on commit 55316e7

Please sign in to comment.