Skip to content

Commit

Permalink
maybe this fixes reconnect breaking stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Techbot121 authored and Meta Construct committed Apr 9, 2024
1 parent 6f897ef commit d65d772
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 18 deletions.
20 changes: 13 additions & 7 deletions app/services/discord/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ export class DiscordBot extends Service {
],
partials: [Discord.Partials.Message, Discord.Partials.Channel, Discord.Partials.Reaction],
});
ready: boolean;

constructor(container: Container) {
super(container);

this.discord.on("ready", async client => {
this.ready = true;
console.log(`'${client.user.username}' Discord Bot has logged in`);
});

this.discord.on("shardDisconnect", () => {
this.ready = false;
});

this.discord.on("warn", console.log);

for (const loadModule of modules) {
Expand All @@ -54,25 +60,25 @@ export class DiscordBot extends Service {
}

getTextChannel(channelId: string): Discord.TextChannel | undefined {
if (!this.discord.isReady()) return;
if (!this.ready) return;
return this.discord.channels.cache.get(channelId) as Discord.TextChannel;
}

async getGuildMember(userId: string): Promise<Discord.GuildMember | undefined> {
if (!this.discord.isReady()) return;
if (!this.ready) return;
return this.discord.guilds.cache.get(this.config.bot.primaryGuildId)?.members.fetch(userId);
}

getGuild(): Discord.Guild | undefined {
if (!this.discord.isReady()) return;
if (!this.ready) return;
return this.discord.guilds.cache.get(this.config.bot.primaryGuildId);
}

async setActivity(
status: string | Discord.Activity | undefined,
options?: Discord.ActivitiesOptions
): Promise<void> {
if (!this.discord.isReady()) return;
if (!this.ready) return;
const activity: Discord.ActivitiesOptions = { name: "Starting up", ...options };
switch (true) {
case status instanceof Discord.Activity: {
Expand All @@ -95,7 +101,7 @@ export class DiscordBot extends Service {
}

async setServerBanner(url: string): Promise<void> {
if (!this.discord.isReady() || !(await this.overLvl2())) return;
if (!this.ready || !(await this.overLvl2())) return;
const guild = this.getGuild();
const response = await axios.get(url, { responseType: "arraybuffer" });
if (!response) return;
Expand All @@ -120,7 +126,7 @@ export class DiscordBot extends Service {
}

async fixEmbeds(msg: Discord.Message): Promise<void> {
if (!this.discord.isReady() || msg.id === lastMessageId) return;
if (!this.ready || msg.id === lastMessageId) return;

if (!ImgurRegex.test(msg.content)) return;

Expand All @@ -145,7 +151,7 @@ export class DiscordBot extends Service {
}

async getLastMotdMsg(): Promise<Discord.Message | undefined> {
if (!this.discord.isReady()) return;
if (!this.ready) return;
const channel = this.getTextChannel(motdConfig.channelId);
if (!channel) return;
return (
Expand Down
2 changes: 1 addition & 1 deletion app/services/discord/modules/shitposting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export default async (bot: DiscordBot) => {
}, MSG_REPLY_REACTION_CLEAR_INTERVAL);

setInterval(async () => {
if (!client.isReady()) return;
if (!bot.ready) return;
const now = Date.now();
if (now - lastMsgTime >= MSG_REPLY_INTERVAL) {
replied = false;
Expand Down
11 changes: 10 additions & 1 deletion app/services/gamebridge/discord/DiscordClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import GameServer from "../GameServer";
import config from "@/config/discord.json";

export default class DiscordClient extends Discord.Client {
gameServer: GameServer;
config = config;
gameServer: GameServer;
ready: boolean;

constructor(gameServer: GameServer, options: Discord.ClientOptions) {
super(options);

this.gameServer = gameServer;

this.on("ready", () => {
this.ready = true;
});

this.on("shardDisconnect", () => {
this.ready = false;
});

this.on("warn", console.log);
}

Expand Down
2 changes: 1 addition & 1 deletion app/services/gamebridge/payloads/AdminNotifyPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class AdminNotifyPayload extends Payload {
let { message } = payload.data;
const { bridge, discord: discordClient } = server;

if (!discordClient.isReady()) return;
if (!discordClient.ready) return;

const guild = discordClient.guilds.cache.get(bridge.config.guildId);
if (!guild) return;
Expand Down
2 changes: 1 addition & 1 deletion app/services/gamebridge/payloads/BanAppealPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class UnbanPayload extends Payload {
const { player, banned, banReason, appeal, unbanTime } = payload.data;
const { bridge, discord: discordClient } = server;

if (!discordClient.isReady()) return;
if (!discordClient.ready) return;

const guild = discordClient.guilds.cache.get(bridge.config.guildId);
if (!guild) return;
Expand Down
2 changes: 1 addition & 1 deletion app/services/gamebridge/payloads/BanPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class BanPayload extends Payload {
const { player, banned, reason, unbanTime } = payload.data;
const { bridge, discord: discordClient } = server;

if (!discordClient.isReady()) return;
if (!discordClient.ready) return;

const guild = discordClient.guilds.cache.get(bridge.config.guildId);
if (!guild) return;
Expand Down
2 changes: 1 addition & 1 deletion app/services/gamebridge/payloads/ChatPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default class ChatPayload extends Payload {
let { content } = payload.data;
const { bridge, discord, discordWH } = server;

if (!discord.isReady()) return;
if (!discord.ready) return;

const guild = discord.guilds.cache.get(discord.config.bot.primaryGuildId);
if (!guild) return;
Expand Down
2 changes: 1 addition & 1 deletion app/services/gamebridge/payloads/JoinLeavePayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class JoinLeavePayload extends Payload {
const { player, reason, spawned } = payload.data;
const { bridge, discord } = server;

if (!discord.isReady()) return;
if (!discord.ready) return;

const guild = discord.guilds.cache.get(discord.config.bot.primaryGuildId);
if (!guild) return;
Expand Down
2 changes: 1 addition & 1 deletion app/services/gamebridge/payloads/NotificationPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class NotificationPayload extends Payload {
const { title, message, color } = payload.data;
const { bridge, discord: discordClient } = server;

if (!discordClient.isReady()) return;
if (!discordClient.ready) return;

const guild = discordClient.guilds.cache.get(bridge.config.guildId);
if (!guild) return;
Expand Down
2 changes: 1 addition & 1 deletion app/services/gamebridge/payloads/StatusPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export default class StatusPayload extends Payload {
}
};

if (discord.isReady() && this.retryCount < 5) {
if (discord.ready && this.retryCount < 5) {
try {
updateStatus();
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion app/services/gamebridge/payloads/UnbanPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class UnbanPayload extends Payload {
const { player, banned, banReason, unbanReason, banTime } = payload.data;
const { bridge, discord: discordClient } = server;

if (!discordClient.isReady()) return;
if (!discordClient.ready) return;

const guild = discordClient.guilds.cache.get(bridge.config.guildId);
if (!guild) return;
Expand Down
2 changes: 1 addition & 1 deletion app/services/gamebridge/payloads/VoteKickPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class NotificationPayload extends Payload {
const { bridge, discord: discordClient } = server;
const steam = bridge.container.getService("Steam");

if (!discordClient.isReady()) return;
if (!discordClient.ready) return;

const guild = discordClient.guilds.cache.get(bridge.config.guildId);
if (!guild) return;
Expand Down

0 comments on commit d65d772

Please sign in to comment.