Skip to content

Commit

Permalink
catch bot init errors
Browse files Browse the repository at this point in the history
  • Loading branch information
a-sync committed Apr 20, 2024
1 parent 91afd8b commit 6ac92c1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 66 deletions.
74 changes: 39 additions & 35 deletions src/discord-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,38 @@ let bot: Client;
export async function init(token: string) {
if (!bot) {
console.log('discord-bot starting...');
bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]
});

bot.on('error', e => {
console.error('discord-bot ERROR', e.message || e);
});

await new Promise<void>((resolve, reject) => {
bot.once('ready', () => {
console.log('discord-bot ready', bot.user);
try {
bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]
});

bot.removeListener('error', reject);
resolve();
bot.on('error', e => {
console.error('discord-bot ERROR', e.message || e);
});

bot.once('error', reject);
await new Promise<void>((resolve, reject) => {
bot.once('ready', () => {
console.log('discord-bot ready', bot.user);

if (DBG) {
bot.on('messageCreate', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
bot.removeListener('error', reject);
resolve();
});
}

return bot.login(token);
});
bot.once('error', reject);

if (DBG) {
bot.on('messageCreate', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
});
}

return bot.login(token);
});
} catch (e: any) {
console.error('discord-bot init ERROR', e.message || e);
}
}

serverInfoMessages.length = 0;
Expand Down Expand Up @@ -173,12 +177,12 @@ class ServerInfoMessage {
embed.setTitle(gs.niceName.slice(0, 256));
embed.setColor(onlineColor as HexColorString);

if (gs.info.game) fields.push({ name: 'Game', value: String(gs.info.game), inline: true});
if (gs.info.map) fields.push({ name: 'Map', value: String(gs.info.map), inline: true});
fields.push({ name: 'Players', value: String(gs.info.playersNum + '/' + gs.info.playersMax), inline: true});
fields.push({ name: 'Address', value: String(gs.info.connect)});
if (gs.info.game) fields.push({ name: 'Game', value: String(gs.info.game), inline: true });
if (gs.info.map) fields.push({ name: 'Map', value: String(gs.info.map), inline: true });
fields.push({ name: 'Players', value: String(gs.info.playersNum + '/' + gs.info.playersMax), inline: true });
fields.push({ name: 'Address', value: String(gs.info.connect) });

if (gs.config.infoText) fields.push({ name: 'Info', value: String(gs.config.infoText).slice(0, 1024)});
if (gs.config.infoText) fields.push({ name: 'Info', value: String(gs.config.infoText).slice(0, 1024) });

if (showPlayersList && gs.info?.players.length > 0) {
const pNames: string[] = [];
Expand All @@ -187,10 +191,10 @@ class ServerInfoMessage {
const pPings: string[] = [];

for (const p of gs.info?.players) {
if (pNames.join('\n').length > 1016
|| pTimes.join('\n').length > 1016
|| pScores.join('\n').length > 1016
|| pPings.join('\n').length > 1016) {
if (pNames.join('\n').length > 1016
|| pTimes.join('\n').length > 1016
|| pScores.join('\n').length > 1016
|| pPings.join('\n').length > 1016) {
if (pNames.length > 0) pNames.pop();
if (pTimes.length > 0) pTimes.pop();
if (pScores.length > 0) pScores.pop();
Expand All @@ -204,10 +208,10 @@ class ServerInfoMessage {
if (p.get('ping') !== undefined) pPings.push(String(p.get('ping') || 0) + ' ms');
}

if (pNames.length > 0) fields.push({ name: 'Name', value: '```\n' + pNames.join('\n').slice(0, 1016) + '\n```', inline: true});
if (pTimes.length > 0) fields.push({ name: 'Time', value: '```\n' + pTimes.join('\n').slice(0, 1016) + '\n```', inline: true});
if (pScores.length > 0) fields.push({ name: 'Score', value: '```\n' + pScores.join('\n').slice(0, 1016) + '\n```', inline: true});
if (pPings.length > 0) fields.push({ name: 'Ping', value: '```\n' + pPings.join('\n').slice(0, 1016) + '\n```', inline: true});
if (pNames.length > 0) fields.push({ name: 'Name', value: '```\n' + pNames.join('\n').slice(0, 1016) + '\n```', inline: true });
if (pTimes.length > 0) fields.push({ name: 'Time', value: '```\n' + pTimes.join('\n').slice(0, 1016) + '\n```', inline: true });
if (pScores.length > 0) fields.push({ name: 'Score', value: '```\n' + pScores.join('\n').slice(0, 1016) + '\n```', inline: true });
if (pPings.length > 0) fields.push({ name: 'Ping', value: '```\n' + pPings.join('\n').slice(0, 1016) + '\n```', inline: true });
}
} else {
embed.setTitle(gs.niceName.slice(0, 245) + ' offline...');
Expand Down
40 changes: 22 additions & 18 deletions src/slack-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,30 @@ let bot: AppType;
export async function init(token: string, appToken: string) {
if (!bot) {
console.log('slack-bot starting...');
bot = new bolt.App({
token,
appToken,
socketMode: true,
logLevel: bolt.LogLevel.ERROR
});

if (DBG) {
bot.message('ping', async ({ message, say }) => {
// Handle only newly posted messages here
if (message.subtype === undefined
|| message.subtype === 'bot_message'
|| message.subtype === 'file_share'
|| message.subtype === 'thread_broadcast') {
await say(`<@${message.user}> pong`);
}
try {
bot = new bolt.App({
token,
appToken,
socketMode: true,
logLevel: bolt.LogLevel.ERROR
});
}

await bot.start();
if (DBG) {
bot.message('ping', async ({ message, say }) => {
// Handle only newly posted messages here
if (message.subtype === undefined
|| message.subtype === 'bot_message'
|| message.subtype === 'file_share'
|| message.subtype === 'thread_broadcast') {
await say(`<@${message.user}> pong`);
}
});
}

await bot.start();
} catch (e: any) {
console.error('slack-bot init ERROR', e.message || e);
}
}

serverInfoMessages.length = 0;
Expand Down
30 changes: 17 additions & 13 deletions src/telegram-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@ let bot: Bot;
export async function init(token: string) {
if (!bot) {
console.log('telegram-bot starting...');
bot = new Bot(token);
try {
bot = new Bot(token);

bot.catch(e => {
console.error('telegram-bot ERROR', e.message || e);
});
bot.catch(e => {
console.error('telegram-bot ERROR', e.message || e);
});

const me = await bot.api.getMe();
console.log('telegram-bot ready', me);
const me = await bot.api.getMe();
console.log('telegram-bot ready', me);

if (DBG) {
bot.on('message:text', ctx => {
if (ctx.message.text === 'ping')
ctx.reply('pong');
});
// bot.command('ping', ctx => ctx.reply('/pong'));
bot.start();
if (DBG) {
bot.on('message:text', ctx => {
if (ctx.message.text === 'ping')
ctx.reply('pong');
});
// bot.command('ping', ctx => ctx.reply('/pong'));
bot.start();
}
} catch (e: any) {
console.error('telegram-bot init ERROR', e.message || e);
}
}

Expand Down

0 comments on commit 6ac92c1

Please sign in to comment.