Skip to content

Commit

Permalink
Merge pull request #76 from Bernd-L:staging
Browse files Browse the repository at this point in the history
Admin role disabled for now; fix #75
  • Loading branch information
Tanja-4732 authored May 30, 2019
2 parents 590c1ee + 705b04c commit 6b599b1
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 108 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "memes-bot",
"version": "0.5.0",
"version": "0.6.0",
"description": "Automates and manages meme channels for Discord guilds",
"scripts": {
"start": "node ./dist/bot.js",
Expand Down
4 changes: 2 additions & 2 deletions sql/queries.pgsql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
delete from "mb_dev".guild;
delete from "mb_dev"."guild_model" where id = '583738575489859604';

SELECT * FROM "mb_dev"."guild" LIMIT 1000;
SELECT * FROM "mb_dev"."guild_model" LIMIT 1000;
7 changes: 4 additions & 3 deletions src/commands/adminRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ export default class AdminRole {
}

static async printAdminRole({ msg }: { msg: Message }): Promise<void> {
log("Printing admin role");

const adminRole = await this.getAdminRole({ msg });
SendMsg.cmdRes({
msg,
status: CmdStatus.INFO,
text: "The admin role is @" + (await this.getAdminRole({ msg })).name
text:
"The admin role is " +
(adminRole == null ? "disabled." : "@" + adminRole.name)
});
}

Expand Down
26 changes: 18 additions & 8 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class Init {
adminRoleRef
}: {
msg: Message;
adminRoleRef: string;
adminRoleRef?: string;
}): Promise<void> {
/**
* Anti-duplicate flag
Expand Down Expand Up @@ -52,13 +52,23 @@ export default class Init {
return;
}

// Register the guild in the db
GuildController.registerGuild({
id: msg.guild.id,
name: msg.guild.name,
adminRoleId: ParseRef.parseRoleRef(adminRoleRef),
cmdChannelId: msg.channel.id
});
try {
// Register the guild in the db
await GuildController.registerGuild({
guild: msg.guild,
name: msg.guild.name,
adminRoleId: null, // TODO set admin role on init
// adminRoleId: ParseRef.parseRoleRef(adminRoleRef),
cmdChannelId: msg.channel.id
});
} catch (error) {
SendMsg.cmdRes({
msg,
status: CmdStatus.ERROR,
text: "Something went wrong.\nThat's all we know."
});
return;
}

// Send success
SendMsg.cmdRes({
Expand Down
62 changes: 0 additions & 62 deletions src/commands/meme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,58 +78,7 @@ export default class Meme {
await memeArray[0].react("👎");

// Register meme
Meme.registerMeme(memeArray); // TODO #58
}

/**
* Registers a meme in the db, and starts watching it immediately
*
*
* @static
* @param {Message[]} memeArray
* @memberof Meme
*/
static registerMeme(memeArray: Message[]): void {
MemeController.add(memeArray);
this.watchMeme(memeArray);
}

/**
* Watches a meme in this instance
*
* This makes sure that downvotes are tracked and events are processed correctly.
* This method should be called for every meme to be tracked after each startup.
*
* @private
* @static
* @param {Message} memeArray
* @memberof Meme
*/
static watchMeme(memeArray: Message[]) {
const downvoteCollector = memeArray[0].createReactionCollector(
(reaction: ReactionEmoji, user: User) => reaction.name === "👎"
);
downvoteCollector.on("collect", this.onDownvote);
}

/**
* Logic for downvote handling
*
* @private
* @static
* @param {MessageReaction} element
* @param {Collector<string, MessageReaction>} collector
* @returns {Promise<void>}
* @memberof Meme
*/
private static async onDownvote(
element: MessageReaction,
collector: Collector<string, MessageReaction>
): Promise<void> {
const downvoteLimit: number = await GuildController.getDownvoteLimit(
element.message.guild
);
if (collector.collected.size > downvoteLimit) this.onLimitExceeded();
}

/**
Expand Down Expand Up @@ -157,17 +106,6 @@ export default class Meme {
}
}

/**
* Remove the meme (including the video, if any)
*
* @private
* @static
* @memberof Meme
*/
private static onLimitExceeded() {
// TODO #58
}

/**
* Sets the meme channel to the desired channel in
* the guild the command (in msg) was sent from
Expand Down
32 changes: 12 additions & 20 deletions src/controllers/guildController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { log } from "util";

export default class GuildController {
public static async registerGuild({
id,
guild,
adminRoleId,
cmdChannelId,
name
}: {
id: string;
guild: Guild;
adminRoleId: string;
cmdChannelId: string;
name: string;
Expand All @@ -19,19 +19,18 @@ export default class GuildController {
* The EntityManager to perform db operations on
*/
const em: EntityManager = getManager();
/**
* The guild-model to be added to the db
*/
const guildToRegister: GuildModel | any = {
// TODO not very nice #61
id: guild.id,
adminRoleId,
cmdChannelId,
name
};

try {
/**
* The guild-model to be added to the db
*/
const guildToRegister: GuildModel | any = {
// TODO not very nice #61
id,
adminRoleId,
cmdChannelId,
name
};

// Add the guild-model to the db
await em.save(GuildModel, guildToRegister);
} catch (error) {
Expand Down Expand Up @@ -159,13 +158,6 @@ export default class GuildController {
);
}

// Validate role
if (guild.roles.get(g.adminRoleId) == null) {
throw new Error(
"Couldn't find role. Make sure to specify an existing role."
);
}

return guild.roles.get(g.adminRoleId);
}

Expand Down
3 changes: 2 additions & 1 deletion src/middlewares/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export default class Cmd {
// 2 Initialize
case 2001:
// Initialize guild
Init.init({ msg, adminRoleRef: ret.stdout.toString() });
Init.init({ msg }); // TODO init with admin role
// Init.init({ msg, adminRoleRef: ret.stdout.toString() });
break;

// 3 Cmd channel
Expand Down
8 changes: 5 additions & 3 deletions src/middlewares/guild/mb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { exit, stdout } from "process";
* @param {string} adminRoleRef The mention of the admin role
*/
function init(adminRoleRef: string) {
// Write the specified adminRoleRef to stdout
stdout.write(adminRoleRef);
// TODO init with admin role
// // Write the specified adminRoleRef to stdout
// stdout.write(adminRoleRef);

// Request init
exit(2001);
Expand Down Expand Up @@ -140,7 +141,8 @@ program

// Initialize
program
.command("initialize <adminRole>")
.command("initialize") // TODO init with admin role
// .command("initialize <adminRole>")
.description(
"Initialize this guild; Sets the cmd channel to the one this command is" +
" issued in, and the admin role to <adminRole>"
Expand Down
8 changes: 4 additions & 4 deletions src/models/guildModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export class GuildModel {
name: string;

/**
* The role which a guild member must have to have the
* commands sent to the bot be executed. Having the
* administrator permission on the guild also works.
* The role which a guild member must have to have the commands
* sent to the bot be executed. Having the administrator
* permission on the guild is also sufficient.
*
* @type {string}
* @memberof GuildModel
*/
@Column()
@Column({ nullable: true })
adminRoleId: string;

/**
Expand Down
8 changes: 5 additions & 3 deletions src/utils/checkCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ export default class CheckCmd {
* @memberof CheckCmd
*/
public static async hasAdminRole(msg: Message): Promise<boolean> {
const hasAdminRole: boolean = msg.member.roles.has(
(await AdminRole.getAdminRole({ msg })).id
);
const adminRole = await AdminRole.getAdminRole({ msg });

if (adminRole == null) return true;

const hasAdminRole: boolean = msg.member.roles.has(adminRole.id);
const isGuildAdmin: boolean = msg.member.permissions.has("ADMINISTRATOR");

return hasAdminRole || isGuildAdmin;
Expand Down

0 comments on commit 6b599b1

Please sign in to comment.