Skip to content

Commit c31cdab

Browse files
Techbot121Meta Construct
authored andcommitted
handle saving in the discordbot methods
1 parent 02e3361 commit c31cdab

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

app/services/discord/index.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,53 @@ export class DiscordBot extends Service {
116116
this.discord.user?.setActivity(activity);
117117
}
118118

119-
async setNickname(name = "Meta", reason?: string): Promise<boolean> {
120-
if (!this.ready || !this.discord.user || name.length > 22) return false;
121-
const nick = name.charAt(0).toUpperCase() + name.slice(1);
122-
this.getGuild()?.members.me?.setNickname(nick + " Construct", reason);
123-
return true;
119+
async setIcon(
120+
path = this.data?.lastDiscordGuildIcon ?? "resources/discord-guild-icons/default.png"
121+
): Promise<boolean> {
122+
if (!this.ready || !this.discord.user) return false;
123+
try {
124+
await this.discord.user.setAvatar(path);
125+
if (this.data) {
126+
this.data.lastDiscordGuildIcon = path;
127+
await this.data.save();
128+
}
129+
return true;
130+
} catch {
131+
return false;
132+
}
124133
}
125134

126-
async getNickname(): Promise<string | undefined> {
127-
if (!this.ready || !this.discord.user) return;
128-
return (await this.getGuildMember(this.discord.user.id))?.nickname?.split(" ")[0];
135+
async setNickname(name = "Meta", reason?: string): Promise<boolean> {
136+
if (!this.ready || !this.discord.user || name.length > 22) return false;
137+
try {
138+
const nick = name.charAt(0).toUpperCase() + name.slice(1);
139+
await this.getGuild()?.members.me?.setNickname(nick + " Construct", reason);
140+
if (this.data) {
141+
this.data.lastDiscordNickName = nick;
142+
await this.data.save();
143+
}
144+
return true;
145+
} catch {
146+
return false;
147+
}
129148
}
130149

131-
async setServerBanner(url: string): Promise<void> {
132-
if (!this.ready || !(await this.overLvl2())) return;
133-
const guild = this.getGuild();
134-
const response = await axios.get(url, { responseType: "arraybuffer" });
135-
if (!response) return;
136-
guild?.setBanner(response.data, "motd");
150+
async setServerBanner(
151+
url = this.data?.lastDiscordBanner ?? null,
152+
reason?: string
153+
): Promise<boolean> {
154+
if (!this.ready || !(await this.overLvl2())) return false;
155+
try {
156+
const guild = this.getGuild();
157+
await guild?.setBanner(url, reason);
158+
if (this.data && url) {
159+
this.data.lastDiscordBanner = url;
160+
await this.data.save();
161+
}
162+
return true;
163+
} catch {
164+
return false;
165+
}
137166
}
138167

139168
async feedMarkov(msg: Discord.Message): Promise<void> {

app/services/discord/modules/discord-events.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export default async (bot: DiscordBot): Promise<void> => {
1818
nicks: ["terror", "detective", "innocent", "trouble", "clue", "banana", "protogen"],
1919
execute: async () =>
2020
(await bot.container.getService("GameBridge")).servers[3]?.sendLua(
21-
`local request = require("gm_request") if request and not request:IsServerGamemode(3,"terrortown") then request:SwitchGamemodeAsync("terrortown",print) end`
22-
),
21+
`local request = require("gm_request") if request and not request:IsServerGamemode(3,"terrortown") then request:SwitchGamemodeAsync("terrortown",print) end`
22+
),
2323
},
2424
];
2525

@@ -69,10 +69,8 @@ export default async (bot: DiscordBot): Promise<void> => {
6969
: false);
7070
if (match) {
7171
const path = join(iconsPath, `${icon}.png`);
72-
await event.guild?.setIcon(path);
72+
await bot.setIcon(path);
7373
await bot.discord.user?.setAvatar(path);
74-
data.lastDiscordNickName = (await bot.getNickname()) ?? "Meta";
75-
await data.save();
7674
await bot.setNickname(
7775
nicks[(Math.random() * nicks.length) | 0],
7876
event.name
@@ -81,11 +79,9 @@ export default async (bot: DiscordBot): Promise<void> => {
8179
break;
8280
}
8381
}
84-
data.lastDiscordBanner = event.guild?.bannerURL() ?? data.lastDiscordBanner;
85-
await data.save();
8682
const banner = event.image;
8783
if (banner) {
88-
await event.guild?.setBanner(banner, "Event banner");
84+
await bot.setServerBanner(banner, "Event banner");
8985
}
9086
break;
9187
}

0 commit comments

Comments
 (0)