Skip to content

Commit

Permalink
feat(#31): switch to .fetch for cache interaction when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Artemus authored and barthofu committed Aug 20, 2022
1 parent bb89831 commit 270b03d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
31 changes: 15 additions & 16 deletions src/api/controllers/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
25 changes: 11 additions & 14 deletions src/services/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

// =================================
Expand Down Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion src/services/Stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/functions/synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 270b03d

Please sign in to comment.