Skip to content

Commit 0672607

Browse files
committed
Move interval next to channel definition
Also removes messageConfig from args and just uses the constant
1 parent 1ac66a4 commit 0672607

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

src/features/scheduled-messages.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ const FREQUENCY = {
1616
};
1717

1818
type MessageConfig = {
19-
interval: number;
20-
postTo: { guildId?: discord.Snowflake; channelId: discord.Snowflake }[];
19+
postTo: {
20+
guildId?: discord.Snowflake;
21+
interval: number;
22+
channelId: discord.Snowflake;
23+
}[];
2124
message: discord.MessageOptions;
2225
};
23-
export const MESSAGE_SCHEDULE: MessageConfig[] = [
26+
const MESSAGE_SCHEDULE: MessageConfig[] = [
2427
/* Example:
2528
{
2629
interval: FREQUENCY.weekly
@@ -40,8 +43,7 @@ export const MESSAGE_SCHEDULE: MessageConfig[] = [
4043
}
4144
*/
4245
{
43-
interval: FREQUENCY.daily,
44-
postTo: [{ channelId: CHANNELS.jobBoard }],
46+
postTo: [{ interval: FREQUENCY.daily, channelId: CHANNELS.jobBoard }],
4547
message: {
4648
content: `Messages must start with [FORHIRE] or [HIRING]. Lead with the location of the position and include LOCAL, REMOTE, INTERN, VISA, etc. and keep the message reasonably formatted & a reasonable length.
4749
@@ -54,8 +56,7 @@ Job postings here do not go through an approval process. Please be diligent and
5456
},
5557
},
5658
{
57-
interval: FREQUENCY.often,
58-
postTo: [{ channelId: CHANNELS.helpJs }],
59+
postTo: [{ interval: FREQUENCY.often, channelId: CHANNELS.helpJs }],
5960
message: {
6061
content: `This channel is good for specific questions about syntax, debugging a small (< ~50 lines of code) snippet of JS, without React involved. Question not getting answered? Maybe it's hard to answer, check out these resources for how to ask a good question:
6162
@@ -65,8 +66,7 @@ How do I ask a good question https://stackoverflow.com/help/how-to-ask
6566
},
6667
},
6768
{
68-
interval: FREQUENCY.often,
69-
postTo: [{ channelId: CHANNELS.helpReact }],
69+
postTo: [{ interval: FREQUENCY.often, channelId: CHANNELS.helpReact }],
7070
message: {
7171
content: `This channel is good for specific questions about React, how React's features work, or debugging a small (< ~50 lines of code) snippet of JS that uses React. Question not getting answered? Maybe it's hard to answer, check out these resources for how to ask a good question:
7272
@@ -76,8 +76,9 @@ How do I ask a good question https://stackoverflow.com/help/how-to-ask
7676
},
7777
},
7878
{
79-
interval: FREQUENCY.moreThanWeekly,
80-
postTo: [{ channelId: CHANNELS.helpReact }],
79+
postTo: [
80+
{ interval: FREQUENCY.moreThanWeekly, channelId: CHANNELS.helpReact },
81+
],
8182
message: {
8283
content: `Check our the other channels too! This is our highest-traffic channel, which may mean your question gets missed as other discussions happen.
8384
@@ -101,8 +102,9 @@ If you see anything that violates our rules, help alert the mods by reacting to
101102
},
102103
},
103104
{
104-
interval: FREQUENCY.moreThanWeekly,
105-
postTo: [{ channelId: CHANNELS.random }],
105+
postTo: [
106+
{ interval: FREQUENCY.moreThanWeekly, channelId: CHANNELS.random },
107+
],
106108
message: {
107109
content: `Have you read our Code of Conduct? <https://www.reactiflux.com/conduct> Is that joke you want to make really in keeping with it? Don't make dad angry.
108110
@@ -113,24 +115,17 @@ If something crosses a line, give it a 👎, or if you'd prefer to remain anonym
113115

114116
export const messages: MessageConfig[] = [];
115117

116-
export const scheduleMessages = (
117-
bot: discord.Client,
118-
messageConfigs: MessageConfig[],
119-
) => {
120-
const scheduledTasks = messageConfigs.map((messageConfig) =>
121-
scheduleTask(messageConfig.interval, () => {
122-
sendMessage(bot, messageConfig);
123-
}),
124-
);
125-
return scheduledTasks;
118+
export const scheduleMessages = (bot: discord.Client) => {
119+
MESSAGE_SCHEDULE.forEach((messageConfig) => sendMessage(bot, messageConfig));
126120
};
127121

128122
const sendMessage = async (
129123
bot: discord.Client,
130124
messageConfig: MessageConfig,
131125
) => {
132-
messageConfig.postTo.forEach(
133-
async ({ guildId = "102860784329052160", channelId }) => {
126+
const { message, postTo } = messageConfig;
127+
postTo.forEach(
128+
async ({ guildId = "102860784329052160", channelId, interval }) => {
134129
const guild = await bot.guilds.fetch(guildId);
135130
const channel = guild.channels.resolve(channelId);
136131

@@ -148,7 +143,9 @@ const sendMessage = async (
148143
);
149144
return;
150145
}
151-
channel.send(messageConfig.message);
146+
scheduleTask(interval, () => {
147+
channel.send(message);
148+
});
152149
},
153150
);
154151
};

src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import setupStats from "./features/stats";
1717
import emojiMod from "./features/emojiMod";
1818
import autodelete from "./features/autodelete-spam";
1919
import { ChannelHandlers } from "./types";
20-
import {
21-
MESSAGE_SCHEDULE,
22-
scheduleMessages,
23-
} from "./features/scheduled-messages";
20+
import { scheduleMessages } from "./features/scheduled-messages";
2421
import tsPlaygroundLinkShortener from "./features/tsplay";
2522

2623
export const bot = new discord.Client({
@@ -154,7 +151,7 @@ bot.on("ready", () => {
154151

155152
bot.user?.setActivity("DMs for !commands", { type: "WATCHING" });
156153

157-
scheduleMessages(bot, MESSAGE_SCHEDULE);
154+
scheduleMessages(bot);
158155
});
159156

160157
bot.on("error", (err) => {

0 commit comments

Comments
 (0)