From c31cdabf6f470e9123abd250305ab7671e9be5f5 Mon Sep 17 00:00:00 2001 From: Techbot121 Date: Sat, 4 Jan 2025 16:27:55 +0100 Subject: [PATCH] handle saving in the discordbot methods --- app/services/discord/index.ts | 57 ++++++++++++++----- .../discord/modules/discord-events.ts | 12 ++-- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/app/services/discord/index.ts b/app/services/discord/index.ts index 1499224..9843922 100644 --- a/app/services/discord/index.ts +++ b/app/services/discord/index.ts @@ -116,24 +116,53 @@ export class DiscordBot extends Service { this.discord.user?.setActivity(activity); } - async setNickname(name = "Meta", reason?: string): Promise { - if (!this.ready || !this.discord.user || name.length > 22) return false; - const nick = name.charAt(0).toUpperCase() + name.slice(1); - this.getGuild()?.members.me?.setNickname(nick + " Construct", reason); - return true; + async setIcon( + path = this.data?.lastDiscordGuildIcon ?? "resources/discord-guild-icons/default.png" + ): Promise { + if (!this.ready || !this.discord.user) return false; + try { + await this.discord.user.setAvatar(path); + if (this.data) { + this.data.lastDiscordGuildIcon = path; + await this.data.save(); + } + return true; + } catch { + return false; + } } - async getNickname(): Promise { - if (!this.ready || !this.discord.user) return; - return (await this.getGuildMember(this.discord.user.id))?.nickname?.split(" ")[0]; + async setNickname(name = "Meta", reason?: string): Promise { + if (!this.ready || !this.discord.user || name.length > 22) return false; + try { + const nick = name.charAt(0).toUpperCase() + name.slice(1); + await this.getGuild()?.members.me?.setNickname(nick + " Construct", reason); + if (this.data) { + this.data.lastDiscordNickName = nick; + await this.data.save(); + } + return true; + } catch { + return false; + } } - async setServerBanner(url: string): Promise { - if (!this.ready || !(await this.overLvl2())) return; - const guild = this.getGuild(); - const response = await axios.get(url, { responseType: "arraybuffer" }); - if (!response) return; - guild?.setBanner(response.data, "motd"); + async setServerBanner( + url = this.data?.lastDiscordBanner ?? null, + reason?: string + ): Promise { + if (!this.ready || !(await this.overLvl2())) return false; + try { + const guild = this.getGuild(); + await guild?.setBanner(url, reason); + if (this.data && url) { + this.data.lastDiscordBanner = url; + await this.data.save(); + } + return true; + } catch { + return false; + } } async feedMarkov(msg: Discord.Message): Promise { diff --git a/app/services/discord/modules/discord-events.ts b/app/services/discord/modules/discord-events.ts index 25fd97e..299cc80 100644 --- a/app/services/discord/modules/discord-events.ts +++ b/app/services/discord/modules/discord-events.ts @@ -18,8 +18,8 @@ export default async (bot: DiscordBot): Promise => { nicks: ["terror", "detective", "innocent", "trouble", "clue", "banana", "protogen"], execute: async () => (await bot.container.getService("GameBridge")).servers[3]?.sendLua( - `local request = require("gm_request") if request and not request:IsServerGamemode(3,"terrortown") then request:SwitchGamemodeAsync("terrortown",print) end` - ), + `local request = require("gm_request") if request and not request:IsServerGamemode(3,"terrortown") then request:SwitchGamemodeAsync("terrortown",print) end` + ), }, ]; @@ -69,10 +69,8 @@ export default async (bot: DiscordBot): Promise => { : false); if (match) { const path = join(iconsPath, `${icon}.png`); - await event.guild?.setIcon(path); + await bot.setIcon(path); await bot.discord.user?.setAvatar(path); - data.lastDiscordNickName = (await bot.getNickname()) ?? "Meta"; - await data.save(); await bot.setNickname( nicks[(Math.random() * nicks.length) | 0], event.name @@ -81,11 +79,9 @@ export default async (bot: DiscordBot): Promise => { break; } } - data.lastDiscordBanner = event.guild?.bannerURL() ?? data.lastDiscordBanner; - await data.save(); const banner = event.image; if (banner) { - await event.guild?.setBanner(banner, "Event banner"); + await bot.setServerBanner(banner, "Event banner"); } break; }