Allow member who invited Muse to do initial setup (#561)

This commit is contained in:
Max Isom 2022-03-12 18:41:53 -05:00 committed by GitHub
parent e1589c3013
commit 20eaed4a16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 4 deletions

View file

@ -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]
### Changed
- Muse will now allow the member who invited Muse to set config options. For this to work, the View Audit Logs permission must be given when inviting Muse. If it isn't given, Muse still works and will contact the owner instead for initial setup.
## [1.4.1] - 2022-03-12
### Changed

View file

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Setting" ADD COLUMN "invitedByUserId" TEXT;

View file

@ -25,6 +25,7 @@ model KeyValueCache {
model Setting {
guildId String @id
invitedByUserId String?
playlistLimit Int @default(50)
secondsToWaitAfterQueueEmpties Int @default(30)
leaveIfNoListeners Boolean @default(true)

View file

@ -152,7 +152,7 @@ export default class {
spinner.text = '📡 updating permissions...';
await Promise.all(this.client.guilds.cache.map(async guild => updatePermissionsForGuild(guild)));
spinner.succeed(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.client.user?.id ?? ''}&scope=bot%20applications.commands&permissions=36700160`);
spinner.succeed(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.client.user?.id ?? ''}&scope=bot%20applications.commands&permissions=36700288`);
});
this.client.on('error', console.error);

View file

@ -6,16 +6,30 @@ import Config from '../services/config.js';
import {prisma} from '../utils/db.js';
import {REST} from '@discordjs/rest';
import {Routes} from 'discord-api-types/v9';
import updatePermissionsForGuild from '../utils/update-permissions-for-guild.js';
export default async (guild: Guild): Promise<void> => {
let invitedBy;
try {
const logs = await guild.fetchAuditLogs({type: 'BOT_ADD'});
invitedBy = logs.entries.find(entry => entry.target?.id === guild.client.user?.id)?.executor;
} catch {}
if (!invitedBy) {
console.warn(`Could not find user who invited Muse to ${guild.name} from the audit logs.`);
}
await prisma.setting.upsert({
where: {
guildId: guild.id,
},
create: {
guildId: guild.id,
invitedByUserId: invitedBy?.id,
},
update: {
invitedByUserId: invitedBy?.id,
},
update: {},
});
const config = container.get<Config>(TYPES.Config);
@ -33,7 +47,12 @@ export default async (guild: Guild): Promise<void> => {
);
}
const owner = await guild.fetchOwner();
await updatePermissionsForGuild(guild);
if (invitedBy) {
await invitedBy.send('👋 Hi! You just invited me to a server. I can\'t be used by your server members until you complete setup by running /config set-role in your server.');
} else {
const owner = await guild.fetchOwner();
await owner.send('👋 Hi! Someone (probably you) just invited me to a server you own. I can\'t be used by your server members until you complete setup by running /config set-role in your server.');
}
};

View file

@ -26,6 +26,15 @@ const updatePermissionsForGuild = async (guild: Guild) => {
permission: false,
},
];
if (settings.invitedByUserId) {
permissions.push({
id: settings.invitedByUserId,
type: 'USER',
permission: true,
});
}
const commands = await guild.commands.fetch();
await guild.commands.permissions.set({fullPermissions: commands.map(command => ({