Skip to content

Commit

Permalink
Add refresh token for each server
Browse files Browse the repository at this point in the history
Ask to authorize user whenever /permit command is used
  • Loading branch information
TrojanerHD committed Mar 18, 2023
1 parent 57fde45 commit bc2cca0
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 166 deletions.
1 change: 1 addition & 0 deletions src/FeatureChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default class FeatureChecker {
permissionRoles: [],
roles: [],
streamers: [],
refreshToken: '',
}).catch(console.error);
} else this.warning(`Limited features due to problems ${guildInfo}`);
}
Expand Down
12 changes: 7 additions & 5 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { ClientRequest, IncomingMessage } from 'http';
import { request, RequestOptions } from 'https';
import RoleChannelManager from './roles/RoleChannelManager';

interface AccessTokens {
[guildId: string]: AccessToken;
}

interface AccessToken {
access_token: string;
expires_at: number;
Expand All @@ -15,7 +19,7 @@ export default abstract class Common {
/**
* The access token for the Discord API
*/
public static _discordAccessToken?: AccessToken;
public static _discordAccessTokens: AccessTokens = {};

/**
* Sanitizes messages for safe use in message contents
Expand All @@ -30,8 +34,8 @@ export default abstract class Common {
* Checks whether the Discord token is valid
* @returns Whether the Discord token is valid
*/
public static accessTokenValid(): boolean {
return (Common._discordAccessToken?.expires_at ?? 0) > Date.now();
public static accessTokenValid(guildId: string): boolean {
return (Common._discordAccessTokens[guildId]?.expires_at ?? 0) > Date.now();
}

/**
Expand All @@ -46,8 +50,6 @@ export default abstract class Common {
) ?? new RoleChannelManager(guild)
);
}


}

/**
Expand Down
24 changes: 18 additions & 6 deletions src/messages/MessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import DiscordClient from '../DiscordClient';
import {
ApplicationCommand,
ApplicationCommandData,
Collection,
Guild,
GuildResolvable,
Message,
} from 'discord.js';
Expand Down Expand Up @@ -46,17 +48,27 @@ export default class MessageHandler {
/**
* Deploys all commands on all servers and for DMs
*/
public static addCommands(): void {
public static addCommands(guild?: Guild): void {
const commands: ApplicationCommandData[] = MessageHandler._commands.map(
(command: Command): ApplicationCommandData => command.deploy
);

const commandPermissions: CommandPermissions = new CommandPermissions();
if (DiscordClient._client.application === undefined) return;

DiscordClient._client.application?.commands
.set(commands)
.then(commandPermissions.onCommandsSet.bind(commandPermissions))
.catch(console.error);
const setCommands: Promise<
Collection<string, ApplicationCommand<{ guild: GuildResolvable }>>
> = DiscordClient._client.application!.commands.set(commands);

if (guild !== undefined) {
const commandPermissions: CommandPermissions = new CommandPermissions(
guild
);
setCommands.then(
commandPermissions.onCommandsSet.bind(commandPermissions)
);
}

setCommands.catch(console.error);
}

/**
Expand Down
72 changes: 30 additions & 42 deletions src/messages/PermitCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Command from './Command';
import GuildSettings from '../settings/GuildSettings';
import { GuildInfo } from '../settings/SettingsDB';
import MessageHandler from './MessageHandler';
import Authentication from './permissions/Authentication';

export default class PermitCommand extends Command {
deploy: ChatInputApplicationCommandData = {
Expand Down Expand Up @@ -69,82 +70,69 @@ export default class PermitCommand extends Command {

const info: GuildInfo = await GuildSettings.settings(interaction.guildId);

let reply: string = '';

switch (args[0].name) {
case 'manage':
const options: CommandInteractionOption<CacheType>[] = args[0].options!;
const newRoleId: string = options[1].role!.id;
const newRoleIdFormatted: string = `<@&${newRoleId}>`;

reply = `Role ${newRoleIdFormatted} `;

const alreadyAdded: boolean = info.permissionRoles.some(
(role: string): boolean => newRoleId === role
);
switch (options[0].value) {
case 'add':
if (alreadyAdded) {
interaction
.reply({
content: `Role ${newRoleIdFormatted} is already a permitted role`,
ephemeral: true,
})
.catch(console.error);
return;
reply += 'is already a permitted role';
break;
}

info.permissionRoles.push(newRoleId);
interaction
.reply({
content: `The role ${newRoleIdFormatted} has been added as a permitted role`,
ephemeral: true,
})
.catch(console.error);

reply += 'has been added as a permitted role';
break;
case 'remove':
if (!alreadyAdded) {
interaction
.reply({
content: `Role ${newRoleIdFormatted} is not a permitted role`,
ephemeral: true,
})
.catch(console.error);
return;
reply += 'is not a permitted role';
break;
}

info.permissionRoles = info.permissionRoles.filter(
(role: string) => newRoleId !== role
);
interaction
.reply({
content: `Role ${newRoleIdFormatted} has been removed from the permitted roles`,
ephemeral: true,
})
.catch(console.error);

reply += 'has been removed from the permitted roles';
break;
}
break;
case 'list':
if (info.permissionRoles.length === 0) {
interaction
.reply({
content: 'Currently, no roles are permitted',
ephemeral: true,
})
.catch(console.error);
return;
reply = 'Currently, no roles are permitted';
break;
}
interaction
.reply({
content: `Currently, the following roles are permitted: ${info.permissionRoles
.map((role: string) => `<@&${role}>`)
.join(', ')}`,
ephemeral: true,
})
.catch(console.error);

reply = `Currently, the following roles are permitted: ${info.permissionRoles
.map((role: string) => `<@&${role}>`)
.join(', ')}`;
break;
}

if (
(await Authentication.getRefreshToken(interaction.guildId)) === undefined
) {
reply += `\nFor this feature to work, please click the following link:\n${Authentication.createURL(
interaction.guildId
)}`;
}

interaction.reply({ content: reply, ephemeral: true });

await GuildSettings.saveSettings(interaction.guild!, info).catch(
console.error
);
MessageHandler.addCommands();
MessageHandler.addCommands(interaction.guild!);
}
}
Loading

0 comments on commit bc2cca0

Please sign in to comment.