Skip to content

Commit

Permalink
handle saving in the discordbot methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Techbot121 authored and Meta Construct committed Jan 4, 2025
1 parent 02e3361 commit c31cdab
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
57 changes: 43 additions & 14 deletions app/services/discord/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,53 @@ export class DiscordBot extends Service {
this.discord.user?.setActivity(activity);
}

async setNickname(name = "Meta", reason?: string): Promise<boolean> {
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<boolean> {
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<string | undefined> {
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<boolean> {
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<void> {
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<boolean> {
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<void> {
Expand Down
12 changes: 4 additions & 8 deletions app/services/discord/modules/discord-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default async (bot: DiscordBot): Promise<void> => {
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`
),
},
];

Expand Down Expand Up @@ -69,10 +69,8 @@ export default async (bot: DiscordBot): Promise<void> => {
: 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
Expand All @@ -81,11 +79,9 @@ export default async (bot: DiscordBot): Promise<void> => {
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;
}
Expand Down

0 comments on commit c31cdab

Please sign in to comment.