Skip to content

Commit

Permalink
Add custom status support
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardozgz committed Nov 27, 2024
1 parent 670541d commit e9c1b8b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ DISCORD_BOT_INSTANCE_DEPLOY_COMMANDS=true
# Bot status, an array of Activities, https://discord.com/developers/docs/topics/gateway-events#activity-object-activity-structure
# The first activity will be shown usually, the rest has a very low chance to appear
DISCORD_BOT_INSTANCE_BOT_PRESENCE_ACTIVITY='[{"name": "Counting everything!", "type": 4}, {"name": "Developing Member Counter!", "state": "YouTube", "url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ", "type": 1}, {"name": "you do horrible things", "type": 3}, {"name": "patpat", "type": 3}]'
# can be 'online' | 'idle' | 'dnd' | 'invisible'
DISCORD_BOT_INSTANCE_BOT_STATUS="online"

# For sending stats to bot listing websites
DISCORD_BOT_INSTANCE_STATS_DBGG_TOKEN=
Expand Down
6 changes: 6 additions & 0 deletions apps/bot/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export const env = createEnv({
)
.parse(JSON.parse(s));
}),
DISCORD_BOT_INSTANCE_BOT_STATUS: z.enum([
"online",
"idle",
"dnd",
"invisible",
]),
DISCORD_BOT_INSTANCE_STATS_DBGG_TOKEN: z.string().optional(),
DISCORD_BOT_INSTANCE_STATS_DBL_TOKEN: z.string().optional(),
DISCORD_BOT_INSTANCE_STATS_BFD_TOKEN: z.string().optional(),
Expand Down
1 change: 1 addition & 0 deletions apps/bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async function main() {
shardCount: env.DISCORD_BOT_INSTANCE_SHARDING_SHARD_COUNT,
maxConcurrency: env.DISCORD_BOT_INSTANCE_SHARDING_SHARD_MAX_CONCURRENCY,
presenceActivity: env.DISCORD_BOT_INSTANCE_BOT_PRESENCE_ACTIVITY,
presenceStatus: env.DISCORD_BOT_INSTANCE_BOT_STATUS,
stats: {
BFDToken: env.DISCORD_BOT_INSTANCE_STATS_BFD_TOKEN,
DBGGToken: env.DISCORD_BOT_INSTANCE_STATS_DBGG_TOKEN,
Expand Down
5 changes: 4 additions & 1 deletion apps/bot/src/jobs/setBotSatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export const setBotStatus = new Job({
time: "0 */5 * * * *",
runOnClientReady: true,
execute: (client) => {
const activities = client.botInstanceOptions.presenceActivity;
const { presenceActivity: activities, presenceStatus: status } =
client.botInstanceOptions;

let activity = activities[0];

if (Math.random() < 0.1) {
Expand All @@ -15,6 +17,7 @@ export const setBotStatus = new Job({
if (!activity) return Promise.resolve();

client.user.setPresence({
status,
activities: [activity],
});

Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/BotInstanceOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type logger from "@mc/logger";
import type { ActivitiesOptions } from "discord.js";
import type { ActivitiesOptions, PresenceStatusData } from "discord.js";

export interface BotInstanceOptions {
id: string;
Expand All @@ -10,6 +10,7 @@ export interface BotInstanceOptions {
maxConcurrency: number;
deployCommands: boolean | string;
presenceActivity: ActivitiesOptions[];
presenceStatus: PresenceStatusData;
stats: {
DBGGToken?: string;
DBLToken?: string;
Expand Down

0 comments on commit e9c1b8b

Please sign in to comment.