Skip to content

Commit

Permalink
Add customizable Bot status (museofficial#599)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Isom <codetheweb@users.noreply.github.com>
Co-authored-by: Max Isom <hi@maxisom.me>
  • Loading branch information
3 people authored Mar 24, 2022
1 parent cf49053 commit 2f9382f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
11 changes: 10 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,14 @@ YOUTUBE_API_KEY=
SPOTIFY_CLIENT_ID=
SPOTIFY_CLIENT_SECRET=

# Optional
############
# Optional #
############

# CACHE_LIMIT=2GB

# See the README for details on the below variables
# BOT_STATUS=
# BOT_ACTIVITY_TYPE=
# BOT_ACTIVITY_URL=
# BOT_ACTIVITY=
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Added a configurable bot status with user defined activities
### Fixed
- Error messages consistently show as `🚫 ope: error`

Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,38 @@ services:

By default, Muse limits the total cache size to around 2 GB. If you want to change this, set the environment variable `CACHE_LIMIT`. For example, `CACHE_LIMIT=512MB` or `CACHE_LIMIT=10GB`.

### Custom Bot Status

In the default state, Muse has the status "Online" and the text "Listening to Music". You can change the status through environment variables:

- `BOT_STATUS`:
- `online` (Online)
- `idle` (Away)
- `dnd` (Do not Disturb)

- `BOT_ACTIVITY_TYPE`:
- `PLAYING` (Playing XYZ)
- `LISTENING` (Listening to XYZ)
- `WATCHING` (Watching XYZ)
- `STREAMING` (Streaming XYZ)

- `BOT_ACTIVITY`: the text that follows the activity type

- `BOT_ACTIVITY_URL` If you use `STREAMING` you MUST set this variable, otherwise it will not work! Here you write a regular YouTube or Twitch Stream URL.

#### Examples

**Muse is watching a movie and is DND**:
- `BOT_STATUS=dnd`
- `BOT_ACTIVITY_TYPE=WATCHING`
- `BOT_ACTIVITY=a movie`

**Muse is streaming Monstercat**:
- `BOT_STATUS=online`
- `BOT_ACTIVITY_TYPE=STREAMING`
- `BOT_ACTIVITY_URL=https://www.twitch.tv/monstercat`
- `BOT_ACTIVITY=Monstercat`

### Bot-wide commands

If you have Muse running in a lot of guilds (10+) you may want to switch to registering commands bot-wide rather than for each guild. (The downside to this is that command updates can take up to an hour to propagate.) To do this, set the environment variable `REGISTER_COMMANDS_ON_BOT` to `true`.
16 changes: 15 additions & 1 deletion src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Client, Collection, User} from 'discord.js';
import {Client, Collection, ExcludeEnum, PresenceStatusData, User} from 'discord.js';
import {inject, injectable} from 'inversify';
import ora from 'ora';
import {TYPES} from './types.js';
Expand All @@ -15,10 +15,12 @@ import {generateDependencyReport} from '@discordjs/voice';
import {REST} from '@discordjs/rest';
import {Routes} from 'discord-api-types/v9';
import updatePermissionsForGuild from './utils/update-permissions-for-guild.js';
import {ActivityTypes} from 'discord.js/typings/enums';

@injectable()
export default class {
private readonly client: Client;
private readonly config: Config;
private readonly token: string;
private readonly shouldRegisterCommandsOnBot: boolean;
private readonly commandsByName!: Collection<string, Command>;
Expand All @@ -29,6 +31,7 @@ export default class {
@inject(TYPES.Config) config: Config,
) {
this.client = client;
this.config = config;
this.token = config.DISCORD_TOKEN;
this.shouldRegisterCommandsOnBot = config.REGISTER_COMMANDS_ON_BOT;
this.commandsByName = new Collection();
Expand Down Expand Up @@ -148,6 +151,17 @@ export default class {
);
}

this.client.user!.setPresence({
activities: [
{
name: this.config.BOT_ACTIVITY,
type: this.config.BOT_ACTIVITY_TYPE as unknown as ExcludeEnum<typeof ActivityTypes, 'CUSTOM'>,
url: this.config.BOT_ACTIVITY_URL === '' ? undefined : this.config.BOT_ACTIVITY_URL,
},
],
status: this.config.BOT_STATUS as PresenceStatusData,
});

// Update permissions
spinner.text = '📡 updating permissions...';
await Promise.all(this.client.guilds.cache.map(async guild => updatePermissionsForGuild(guild)));
Expand Down
8 changes: 8 additions & 0 deletions src/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const CONFIG_MAP = {
DATA_DIR,
CACHE_DIR: path.join(DATA_DIR, 'cache'),
CACHE_LIMIT_IN_BYTES: xbytes.parseSize(process.env.CACHE_LIMIT ?? '2GB'),
BOT_STATUS: process.env.BOT_STATUS ?? 'online',
BOT_ACTIVITY_TYPE: process.env.BOT_ACTIVITY_TYPE ?? 'LISTENING',
BOT_ACTIVITY_URL: process.env.BOT_ACTIVITY_URL ?? '',
BOT_ACTIVITY: process.env.BOT_ACTIVITY ?? 'music',
} as const;

@injectable()
Expand All @@ -29,6 +33,10 @@ export default class Config {
readonly DATA_DIR!: string;
readonly CACHE_DIR!: string;
readonly CACHE_LIMIT_IN_BYTES!: number;
readonly BOT_STATUS!: string;
readonly BOT_ACTIVITY_TYPE!: string;
readonly BOT_ACTIVITY_URL!: string;
readonly BOT_ACTIVITY!: string;

constructor() {
for (const [key, value] of Object.entries(CONFIG_MAP)) {
Expand Down

0 comments on commit 2f9382f

Please sign in to comment.