Skip to content

Commit

Permalink
Fixed Apollo not working, fix #153
Browse files Browse the repository at this point in the history
  • Loading branch information
CreepPork committed Dec 3, 2020
1 parent beeee9a commit a6fe3b6
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 178 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@
- Pings a specific role when a configurable threshold is reached (also provides role assignment via a reaction)

## Requirements
- Node >= 10.15.3
- NPM >= 6.4.1
- Node >= **12.x**
- NPM

## Upgrade guide to v3
1. Upgrade your Node.js version to 12.x or above
2. In `https://discord.com/developers/applications/xxx/bot` page, enable the toggle `SERVER MEMBERS INTENT`. This is now required for Apollo to fetch your server users, due to recent API changes.

## Setup
- Get the code
Expand Down
103 changes: 68 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 27 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"name": "apollo",
"version": "2.2.0",
"description": "Discord bot that allows to get status updates on your server.",
"main": "dist/main.js",
"repository": "https://github.com/CreepPork/Apollo",
"scripts": {
"lint": "tslint -c tslint.json 'src/**/.ts'",
"start": "node .",
"build": "tsc -p tsconfig.json",
"watch": "npm run build -- --watch"
},
"author": "CreepPork_LV",
"license": "MIT",
"devDependencies": {
"@types/dotenv": "^6.1.1",
"@types/node": "^12.7.9",
"tslint": "^5.20.0",
"typescript": "^3.6.3"
},
"dependencies": {
"@sentry/integrations": "^5.6.1",
"@sentry/node": "^5.6.2",
"discord.js": "^11.5.1",
"dotenv": "^8.0.0",
"gamedig": "^2.0.14",
"moment": "^2.24.0"
}
"name": "apollo",
"version": "3.0.0",
"description": "Discord bot that allows to get status updates on your server.",
"main": "dist/main.js",
"repository": "https://github.com/CreepPork/Apollo",
"scripts": {
"lint": "tslint -c tslint.json 'src/**/.ts'",
"start": "node .",
"build": "tsc -p tsconfig.json",
"watch": "npm run build -- --watch"
},
"author": "CreepPork_LV",
"license": "MIT",
"devDependencies": {
"@types/dotenv": "^6.1.1",
"@types/node": "^12.7.9",
"tslint": "^5.20.0",
"typescript": "^3.6.3"
},
"dependencies": {
"@sentry/integrations": "^5.6.1",
"@sentry/node": "^5.6.2",
"discord.js": "^12.5.1",
"dotenv": "^8.0.0",
"gamedig": "^2.0.14",
"moment": "^2.24.0"
}
}
32 changes: 17 additions & 15 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ export default class Bot {
}

public start() {
this.discord.client.on('ready', () => {
this.refresh();
this.discord.client.on('ready', async () => {
console.info('Apollo is ready.');
console.info('Performing first-start refresh.');

this.refreshLoop();
await this.refresh();

console.info('Apollo is ready.');
console.info('First-time refresh done, starting refresh loop.');
this.refreshLoop();
});

this.discord.client.on('message', message => this.onMessage(message));
Expand All @@ -74,7 +76,7 @@ export default class Bot {
this.refresh(true);
}
} else if (message.content === Environment.get('maintenance_toggle_command', 'string', true)) {
if (this.discord.doesUserHaveServerManagerPermissions(message.member)) {
if (message.member && this.discord.doesUserHaveServerManagerPermissions(message.member)) {
this.toggleMaintenanceMode(message);
} else {
this.replyNoPermissions(message);
Expand Down Expand Up @@ -119,7 +121,7 @@ export default class Bot {
}

private refreshBotWithRolePermissions(message: Message) {
if (this.discord.doesUserHaveServerManagerPermissions(message.member)) {
if (message.member && this.discord.doesUserHaveServerManagerPermissions(message.member)) {
this.refresh(true);
} else {
this.replyNoPermissions(message);
Expand All @@ -137,12 +139,12 @@ export default class Bot {
private async refresh(forceNewMessage = false) {
if (this.maintenanceMode) { return; }

await this.discord.startThinking();
this.discord.startThinking();

const messageId = Settings.get().messageId;
const query = await this.query;

if (messageId && ! forceNewMessage) {
if (messageId && !forceNewMessage) {
this.discord.editMessage(messageId, await this.discord.createRichEmbed(query)).then(() => {
this.discord.setActivity(query ? 'ok' : 'serverError', query);
this.discord.stopThinking();
Expand Down Expand Up @@ -174,8 +176,8 @@ export default class Bot {

const errorMessageId = Settings.get().errorMessageId;

if (! query) {
if (! errorMessageId) {
if (!query) {
if (!errorMessageId) {
this.postErrorMessage();
}
} else {
Expand All @@ -187,7 +189,7 @@ export default class Bot {
const minPlayers = Environment.get<number>('minimum_player_count_for_ping', 'number', true);
if (query.players.length >= minPlayers) {
// Don't duplicate the message
if (! Settings.get().pingMessageId) {
if (!Settings.get().pingMessageId) {
const pingMessageId = await this.discord.postMessage(
this.discord.generatePing(Environment.get('reaction_role_id')) +
` ${minPlayers} ${Environment.locale.pingMessage}`,
Expand All @@ -202,9 +204,9 @@ export default class Bot {
if (settings.pingMessageId && settings.lastPingMessageTime) {
if (Time.getDiffMinutes(new Date(), new Date(settings.lastPingMessageTime)) >=
Environment.get<number>('timeout_between_player_pings_in_minutes', 'number', false)) {
this.discord.deleteMessage(settings.pingMessageId);
this.discord.deleteMessage(settings.pingMessageId);

Settings.set('pingMessageId', undefined);
Settings.set('pingMessageId', undefined);
}
}
}
Expand All @@ -222,8 +224,8 @@ export default class Bot {
private refreshLoop() {
const timeToWait = Environment.get<number>('time_to_check_minutes', 'number') * 1000 * 60;

setInterval(() => {
this.refresh();
setInterval(async () => {
await this.refresh();
}, timeToWait);
}

Expand Down
Loading

0 comments on commit a6fe3b6

Please sign in to comment.