diff --git a/src/api/controllers/bot.ts b/src/api/controllers/bot.ts index d81ecd25..98b2bc75 100644 --- a/src/api/controllers/bot.ts +++ b/src/api/controllers/bot.ts @@ -128,30 +128,29 @@ export class BotController extends BaseController { return } - // get the first channel where the bot has the permission to create an invite, - // because `.createInvite()` is a method for a channel, not a guild - const channel = guild.channels.cache - .filter((channel) => - guild.members.cache.get(this.client.user?.id || '')?.permissionsIn(channel).has(PermissionsBitField.Flags.CreateInstantInvite) || false - && [ChannelType.GuildText, ChannelType.GuildVoice, ChannelType.GuildNews].includes(channel.type) - ) - .first() as BaseGuildTextChannel | BaseGuildVoiceChannel | NewsChannel | undefined - - if (!channel) { - this.error(ctx, 'Missing permission to create an invite in this guild', 401) - return + const guildChannels = await guild.channels.fetch() + for (const channel of guildChannels.values()) { + if ( + (guild.members.me?.permissionsIn(channel).has(PermissionsBitField.Flags.CreateInstantInvite) || false) && + [ChannelType.GuildText, ChannelType.GuildVoice, ChannelType.GuildNews].includes(channel.type) + ) { + const invite = await (channel as BaseGuildTextChannel | BaseGuildVoiceChannel | NewsChannel | undefined)?.createInvite() + if(invite) { this.ok(ctx, invite); return } + + this.error(ctx, 'Could not create an invite', 500) + return + } } - const invite = await channel.createInvite() - - this.ok(ctx, invite) + this.error(ctx, 'Missing permission to create an invite in this guild', 401) + return } @Get('/users') async users(ctx: Context) { const users: any[] = [], - guilds = this.client.guilds.cache.map(guild => guild) + guilds = this.client.guilds.cache.values() for (const guild of guilds) { diff --git a/src/services/Logger.ts b/src/services/Logger.ts index f3a9ac64..8b73c8fe 100644 --- a/src/services/Logger.ts +++ b/src/services/Logger.ts @@ -77,20 +77,17 @@ export class Logger { fs.appendFileSync(fileName, `${templatedMessage}\n`) } - discordChannel(channelId: string, message: string = '', level?: typeof this.levels[number]) { + async discordChannel(channelId: string, message: string = '', level?: typeof this.levels[number]) { + const channel = await this.client.channels.fetch(channelId) - waitForDependency(Client).then(client => { - const channel = client.channels.cache.get(channelId) + if ( + channel instanceof TextChannel + || channel instanceof ThreadChannel + ) { - if ( - channel instanceof TextChannel - || channel instanceof ThreadChannel - ) { - - // TODO: add support for embeds depending on the level - channel.send(message) - } - }) + // TODO: add support for embeds depending on the level + channel.send(message) + } } // ================================= @@ -197,8 +194,8 @@ export class Logger { type === 'DELETE_GUILD' ? 'has been deleted' : type === 'RECOVER_GUILD' ? 'has been recovered' : '' - waitForDependency(Client).then(client => { - const guild = client.guilds.cache.get(guildId) + waitForDependency(Client).then(async client => { + const guild = await client.guilds.fetch(guildId) const message = `(${type}) Guild ${guild ? `${guild.name} (${guildId})` : guildId} ${additionalMessage}` const chalkedMessage = oneLine` diff --git a/src/services/Stats.ts b/src/services/Stats.ts index e5909138..2d96cb15 100644 --- a/src/services/Stats.ts +++ b/src/services/Stats.ts @@ -171,7 +171,7 @@ export class Stats { for (const guild of guilds) { - const discordGuild = this.client.guilds.cache.get(guild.id) + const discordGuild = await this.client.guilds.fetch(guild.id) const commandsCount = await this.db.getRepo(Stat).count({ ...allInteractions, diff --git a/src/utils/functions/synchronizer.ts b/src/utils/functions/synchronizer.ts index 493f3d20..e428ccc1 100644 --- a/src/utils/functions/synchronizer.ts +++ b/src/utils/functions/synchronizer.ts @@ -43,7 +43,7 @@ export const syncGuild = async (guildId: string, client: Client) => { const guildRepo = db.getRepo(Guild), guildData = await guildRepo.findOne({ id: guildId, deleted: false }) - const fetchedGuild = client.guilds.cache.get(guildId) + const fetchedGuild = await client.guilds.fetch(guildId) //check if this guild exists in the database, if not it creates it (or recovers it from the deleted ones) if (!guildData) {