-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathdiscord.service.ts
More file actions
158 lines (128 loc) · 5.93 KB
/
Copy pathdiscord.service.ts
File metadata and controls
158 lines (128 loc) · 5.93 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Block } from 'bitcoinjs-lib';
import { Client, Collection, Events, GatewayIntentBits, REST, Routes, SlashCommandBuilder, TextChannel } from 'discord.js';
import { NumberSuffix } from '../utils/NumberSuffix';
interface IDiscordCommand {
data: SlashCommandBuilder;
execute(interaction: any): Promise<void>;
}
const subscribeCommand = {
data: new SlashCommandBuilder()
.setName('subscribe')
.setDescription('Subscribes you to specified address'),
async execute(interaction) {
await interaction.reply('Work In Progress');
}
}
const commands = [
subscribeCommand
]
@Injectable()
export class DiscordService implements OnModuleInit {
private token: string;
private clientId: string;
private guildId: string;
private channelId: string;
private diffNotifications: boolean;
private numberSuffix: NumberSuffix;
private bot: Client;
private commandCollection: Collection<string, IDiscordCommand>;
constructor(private readonly configService: ConfigService) {
if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') {
this.token = this.configService.get('DISCORD_BOT_TOKEN');
this.clientId = this.configService.get('DISCORD_BOT_CLIENTID');
this.guildId = this.configService.get('DISCORD_BOT_GUILD_ID');
this.channelId = this.configService.get('DISCORD_BOT_CHANNEL_ID')
if (this.token == null || this.token.length < 1 ||
this.clientId == null || this.clientId.length < 1 ||
this.guildId == null || this.guildId.length < 1 ||
this.channelId == null || this.channelId.length < 1
) {
return;
}
console.log('discord init')
this.commandCollection = new Collection();
commands.forEach(command => {
this.commandCollection.set(command.data.name, command);
});
this.bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });
this.bot.login(this.token);
this.numberSuffix = new NumberSuffix();
this.diffNotifications = (this.configService.get('DISCORD_DIFF_NOTIFICATIONS').toLowerCase() == 'true') || false;
}
}
async onModuleInit(): Promise<void> {
if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') {
if (this.bot == null) {
return;
}
await this.registerCommands();
this.bot.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = this.commandCollection.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});
}
}
private async registerCommands() {
if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') {
const rest = new REST().setToken(this.token);
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(this.clientId, this.guildId),
{ body: commands.map(c => c.data.toJSON()) },
) as any;
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
}
}
public async notifyRestarted() {
if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') {
if (this.bot == null) {
return;
}
const guild = await this.bot.guilds.fetch(this.guildId);
const channel = await guild.channels.fetch(this.channelId) as TextChannel;
channel.send(`Server Restarted.`);
}
}
public async notifySubscribersBlockFound(height: number, block: Block, message: string) {
if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') {
if (this.bot == null) {
return;
}
const guild = await this.bot.guilds.fetch(this.guildId);
const channel = await guild.channels.fetch(this.channelId) as TextChannel;
channel.send(`Block Found! Result: ${message}, Height: ${height}`);
}
}
public async notifySubscribersBestDiff(submissionDifficulty: number) {
if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') {
if (this.bot == null || this.diffNotifications == false) {
return;
}
const guild = await this.bot.guilds.fetch(this.guildId);
const channel = await guild.channels.fetch(this.channelId) as TextChannel;
channel.send(`New Best Diff! Result: ${this.numberSuffix.to(submissionDifficulty)}`);
}
}
}