-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.ts
96 lines (78 loc) · 2.49 KB
/
bot.ts
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
import log from 'loglevel';
import {
AudioPlayer, AudioPlayerStatus, createAudioResource, entersState, joinVoiceChannel,
StreamType, VoiceConnectionStatus
} from '@discordjs/voice';
import * as Discord from 'discord.js';
import { GatewayIntentBits } from 'discord.js';
import { createDiscordJSAdapter } from './adapter';
import { BotContext } from './botcontext';
import { makeCommander } from './command';
export interface BotConfig {
maxConcurrentPlayers: number
disconnectAferInactivityMs: number
dataDir: string
myInstantsEnabled: boolean
mediawikiCDNEnabled: boolean
fandomDomains: {[name:string]: string}
bannedSounds: string[]
}
export function playTaunt(player: AudioPlayer, file: string) {
const resource = createAudioResource(file, {
inputType: StreamType.Arbitrary,
});
player.play(resource);
return entersState(player, AudioPlayerStatus.Playing, 5e3);
}
export async function connectToChannel(channel: Discord.VoiceBasedChannel) {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: createDiscordJSAdapter(channel),
});
try {
await entersState(connection, VoiceConnectionStatus.Ready, 30e3);
return connection;
} catch (error) {
try {
connection.destroy();
} catch (error) {
console.log("error while destroying connection", error)
}
throw error;
}
}
export function createBot(config: BotConfig) {
log.info("Initializing with", config);
const client = new Discord.Client({intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates]});
const botContext = new BotContext(config)
const commander = makeCommander(botContext)
client.on("messageCreate", async (message) => {
if (message.inGuild()) {
await commander.evaluate(client, message)
}
});
// when someone joins a channel
client.on("voiceStateUpdate", async (oldState, newState) => {
if (oldState.channelId != newState.channelId && newState.channelId) {
if (!newState.guild || newState.member?.user.bot) {
return;
}
log.info("user entered voice channel (userId,channelId:)", newState.id, newState.channelId)
try {
const channel = newState.channel;
if (channel) {
await commander.playPersonalTheme(newState.guild.id, newState.id, channel)
}
}
catch (error) {
log.error(error);
}
}
})
return client;
}