mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 17:15:03 -07:00
discord
This commit is contained in:
+5
-1
@@ -13,6 +13,8 @@ import { TelegramSubscriptionsModule } from './ORM/telegram-subscriptions/telegr
|
||||
import { BitcoinRpcService } from './services/bitcoin-rpc.service';
|
||||
import { BlockTemplateService } from './services/block-template.service';
|
||||
import { CleanupService } from './services/cleanup.service';
|
||||
import { DiscordService } from './services/discord.service';
|
||||
import { NotificationService } from './services/notification.service';
|
||||
import { StratumV1Service } from './services/stratum-v1.service';
|
||||
import { TelegramService } from './services/telegram.service';
|
||||
|
||||
@@ -42,11 +44,13 @@ const ORMModules = [
|
||||
AddressController
|
||||
],
|
||||
providers: [
|
||||
DiscordService,
|
||||
CleanupService,
|
||||
StratumV1Service,
|
||||
TelegramService,
|
||||
BitcoinRpcService,
|
||||
BlockTemplateService
|
||||
BlockTemplateService,
|
||||
NotificationService
|
||||
],
|
||||
})
|
||||
export class AppModule {
|
||||
|
||||
@@ -5,7 +5,7 @@ import * as crypto from 'crypto';
|
||||
import { Socket } from 'net';
|
||||
import PromiseSocket from 'promise-socket';
|
||||
import { combineLatest, firstValueFrom, interval, startWith, takeUntil } from 'rxjs';
|
||||
import { TelegramService } from 'src/services/telegram.service';
|
||||
import { NotificationService } from 'src/services/notification.service';
|
||||
|
||||
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
|
||||
import { ClientEntity } from '../ORM/client/client.entity';
|
||||
@@ -49,7 +49,7 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
private readonly bitcoinRpcService: BitcoinRpcService,
|
||||
private readonly clientService: ClientService,
|
||||
private readonly clientStatisticsService: ClientStatisticsService,
|
||||
private readonly telegramService: TelegramService
|
||||
private readonly notificationService: NotificationService
|
||||
) {
|
||||
super();
|
||||
|
||||
@@ -347,7 +347,7 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
console.log('!!! BLOCK FOUND !!!');
|
||||
const blockHex = updatedJobBlock.toHex(false);
|
||||
this.bitcoinRpcService.SUBMIT_BLOCK(blockHex);
|
||||
await this.telegramService.notifySubscribersBlockFound(this.clientAuthorization.address);
|
||||
await this.notificationService.notifySubscribersBlockFound(this.clientAuthorization.address);
|
||||
}
|
||||
try {
|
||||
await this.statistics.addSubmission(this.entity, submissionHash, this.sessionDifficulty);
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Client, Collection, Events, GatewayIntentBits, REST, Routes, SlashCommandBuilder, TextChannel } from 'discord.js';
|
||||
|
||||
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 bot: Client;
|
||||
private commandCollection: Collection<string, IDiscordCommand>;
|
||||
|
||||
|
||||
constructor(private readonly configService: ConfigService) {
|
||||
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')
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
|
||||
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() {
|
||||
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 notifySUbscribersBlockFound() {
|
||||
const guild = await this.bot.guilds.fetch(this.guildId);
|
||||
const channel = await guild.channels.fetch(this.channelId) as TextChannel;
|
||||
channel.send("Block Found!")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
|
||||
import { DiscordService } from './discord.service';
|
||||
import { TelegramService } from './telegram.service';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class NotificationService implements OnModuleInit {
|
||||
|
||||
constructor(
|
||||
private readonly telegramService: TelegramService,
|
||||
private readonly discordService: DiscordService
|
||||
) { }
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
|
||||
}
|
||||
|
||||
public async notifySubscribersBlockFound(address: string) {
|
||||
await this.discordService.notifySUbscribersBlockFound();
|
||||
await this.telegramService.notifySubscribersBlockFound(address);
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,8 @@ import { ClientStatisticsService } from '../ORM/client-statistics/client-statist
|
||||
import { ClientService } from '../ORM/client/client.service';
|
||||
import { BitcoinRpcService } from './bitcoin-rpc.service';
|
||||
import { BlockTemplateService } from './block-template.service';
|
||||
import { NotificationService } from './notification.service';
|
||||
import { StratumV1JobsService } from './stratum-v1-jobs.service';
|
||||
import { TelegramService } from './telegram.service';
|
||||
|
||||
|
||||
@Injectable()
|
||||
@@ -19,7 +19,7 @@ export class StratumV1Service implements OnModuleInit {
|
||||
private readonly blockTemplateService: BlockTemplateService,
|
||||
private readonly clientService: ClientService,
|
||||
private readonly clientStatisticsService: ClientStatisticsService,
|
||||
private readonly telegramService: TelegramService
|
||||
private readonly notificationService: NotificationService
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ export class StratumV1Service implements OnModuleInit {
|
||||
this.bitcoinRpcService,
|
||||
this.clientService,
|
||||
this.clientStatisticsService,
|
||||
this.telegramService
|
||||
this.notificationService
|
||||
);
|
||||
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@ export class TelegramService implements OnModuleInit {
|
||||
private readonly configService: ConfigService,
|
||||
private readonly telegramSubscriptionsService: TelegramSubscriptionsService
|
||||
) {
|
||||
const token: string = this.configService.get('TELEGRAM_BOT_TOKEN');
|
||||
if (token.length < 1) {
|
||||
const token: string | null = this.configService.get('TELEGRAM_BOT_TOKEN');
|
||||
if (token == null || token.length < 1) {
|
||||
console.log('No Telegram token found');
|
||||
return;
|
||||
}
|
||||
@@ -26,6 +26,10 @@ export class TelegramService implements OnModuleInit {
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
|
||||
if (this.bot == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
this.bot.onText(/\/subscribe/, async (msg) => {
|
||||
const address = msg.text.split('/subscribe ')[1];
|
||||
if (validate(address) == false) {
|
||||
@@ -46,6 +50,10 @@ export class TelegramService implements OnModuleInit {
|
||||
}
|
||||
|
||||
public async notifySubscribersBlockFound(address: string) {
|
||||
if (this.bot == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address);
|
||||
subscribers.forEach(subscriber => {
|
||||
this.bot.sendMessage(subscriber.telegramChatId, 'You found a block!');
|
||||
|
||||
Reference in New Issue
Block a user