Skip to content

Commit

Permalink
Complete rolling media
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheemap committed Feb 14, 2020
1 parent d61c837 commit fef4e48
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 41 deletions.
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/src/app.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/**/*.js"
]
}
]
}
16 changes: 13 additions & 3 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@types/glob": "^7.1.1",
"@types/node": "^13.1.8",
"debug": "^4.1.1",
"discord.js": "^11.5.1",
"glob": "^7.1.6",
"knex": "^0.20.8",
Expand Down
144 changes: 122 additions & 22 deletions src/commands/media.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import knex, { DbServer, DbUser } from "../common/db";
import {
Message,
MessageReaction,
Attachment,
MessageAttachment,
Channel,
} from "discord.js";
import { MediaConfig } from "../common/config";
import knex from "../common/db";
import { Message, MessageReaction } from "discord.js";
import { GetUserIdFromMessage, GetTimestamp } from "../common/utilities";
import { logger } from "../common/logger";
import { type } from "os";

const sleep = ms => new Promise(r => setTimeout(r, 100000));
const DEFAULTPREFIX = "!";

var config: ChannelConfig;
Expand All @@ -28,14 +21,13 @@ export function OnMessage(msg: Message) {
.then(configRow => {
config = configRow;
prefix = config?.Prefix || DEFAULTPREFIX;
console.log(args);
switch (args[0]) {
case `${prefix}mediaconfig`:
Configure(msg, args);
break;

case `${prefix}roll`:
RollMedia(msg);
RollMedia(msg, args);
break;

default:
Expand Down Expand Up @@ -187,11 +179,6 @@ function GetSettingsFromArgs(chanConfig: ChannelConfig, args: string[]) {
}
chanConfig.MinimumPoints = min;
break;

case "-dr":
case "--dont-remove-at-minimum":
chanConfig.RemoveAtMinimum = 0;
break;
}
} catch {
errors.push(a);
Expand All @@ -211,19 +198,132 @@ function GetSettingsOrDefaultFromArgs(args: string[]) {
ChannelConfigId: null,
MediaChannelId: null,
RollChannelId: null,
CurrentlyRolling: 0,
CreatedBy: null,
DateCreated: null,
DateUpdated: null,
Prefix: DEFAULTPREFIX,
BufferPercentage: 0.75,
MaximumPoints: 20,
MinimumPoints: -5,
RemoveAtMinimum: 1,
};
return GetSettingsFromArgs(chanConfig, args);
}

function RollMedia(msg: Message) {}
function RollMedia(msg: Message, args: string[]) {
if (
typeof config === "undefined" ||
msg.channel.id !== config.RollChannelId
) {
msg.reply("Rolling is not configured in this channel.");
return;
}
if (config.CurrentlyRolling == 1) {
msg.reply("Cant roll twice at once! Wait for the other roll to end.");
return;
}

let count = Math.abs(parseInt(args[1]) || 1);
let interval = Math.abs(parseInt(args[2]) || 3);
knex<ChannelConfig>("ChannelConfig")
.update({ CurrentlyRolling: 1 })
.then(() => {
msg.channel.send(
`Rolling ${count} medias with an interval of ${interval} seconds`
);
logger.info(
`Rolling ${count} medias with an interval of ${interval} seconds in channel config ${config.ChannelConfigId}`
);
SelectRollableMedia(SendMedia, msg, interval, count, 0);
});
}

function SendMedia(
media: Media,
msg: Message,
interval: number,
count: number,
currentCount: number
) {
if (media.Url.indexOf("cdn.discordapp.com") != -1) {
msg.channel.send({ file: media.Url }).then(sentMsg => {
SaveMediaRoll(media, sentMsg as Message, msg);
});
} else {
msg.channel.send(media.Url).then(sentMsg => {
SaveMediaRoll(media, sentMsg as Message, msg);
});
}
if (currentCount < count)
setTimeout(function() {
SelectRollableMedia(SendMedia, msg, interval, count, currentCount);
}, interval * 1000);
else {
knex<ChannelConfig>("ChannelConfig")
.update("CurrentlyRolling", 0)
.where("ChannelConfigId", config.ChannelConfigId)
.then(() => logger.info("Finished roll"));
}
}

function SaveMediaRoll(media: Media, mediaMsg: Message, originalMsg: Message) {
GetUserIdFromMessage(originalMsg, userId => {
knex<MediaRoll>("MediaRoll")
.insert({
MediaId: media.MediaId,
MessageId: mediaMsg.id,
CreatedBy: userId,
DateCreated: GetTimestamp(),
})
.then(() =>
logger.debug(`Saved MediaRoll entry from userId ${userId}`)
);
});
}

function SelectRollableMedia(
callback: Function,
msg: Message,
interval: number,
count: number,
currentCount: number
) {
let bufferCount = 0;
currentCount++;

knex<Media>("Media")
.select("Media.MediaId")
.sum("IsUpvote as Points")
.where("ConfigId", config.ChannelConfigId)
.leftJoin("MediaVote", "Media.MediaId", "MediaVote.MediaId")
.groupBy("MediaVote.MediaId")
.having("Points", ">", config.MinimumPoints)
.orHavingRaw("`Points` is null")
.then(rows => {
bufferCount = Math.round(rows.length * config.BufferPercentage);
if (bufferCount >= rows.length) {
bufferCount = rows.length - 1;
}
knex<Media>("Media")
.select("Media.MediaId", "Media.Url")
.leftJoin(
knex.raw(
"(select `MediaId`, `MediaRollId` from `MediaRoll` order by `MediaRollId` desc limit ?) MediaRoll",
[bufferCount]
),
"Media.MediaId",
"MediaRoll.MediaId"
)
.whereNull("MediaRoll.MediaRollId")
.then(rollableMedias => {
var media: Media =
rollableMedias[
Math.floor(Math.random() * rollableMedias.length)
];
callback(media, msg, interval, count, currentCount);
});
});
}

function ProcessMessage(msg: Message) {
if (msg.channel.id != config?.MediaChannelId) return;
Expand All @@ -250,7 +350,7 @@ function SaveMedia(url: string, messageId: string, userId: number) {
CreatedBy: userId,
DateCreated: GetTimestamp(),
})
.then(() => logger.info(`Saved meme sent by userId ${userId}`));
.then(() => logger.info(`Saved media sent by userId ${userId}`));
}

// Initialize tables
Expand All @@ -263,7 +363,7 @@ export interface ChannelConfig {
BufferPercentage: number;
MaximumPoints: number;
MinimumPoints: number;
RemoveAtMinimum: number;
CurrentlyRolling: number;
CreatedBy: number;
DateCreated: number;
DateUpdated: number;
Expand All @@ -281,7 +381,7 @@ knex.schema.hasTable("ChannelConfig").then(exists => {
t.decimal("BufferPercentage");
t.integer("MaximumPoints");
t.integer("MinimumPoints");
t.integer("RemoveAtMinimum");
t.integer("CurrentlyRolling");
t.integer("CreatedBy");
t.integer("DateCreated");
t.integer("DateUpdated");
Expand Down
1 change: 1 addition & 0 deletions src/common/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const knex = knexjs({
connection: {
filename: sqliteFileLocation,
},
debug: true,
useNullAsDefault: true,
log: {
warn(message) {
Expand Down
28 changes: 12 additions & 16 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"sourceMap": true,
"removeComments": true,
"preserveConstEnums": true,
},
"include": [
"src/**/*",
],
"exclude": [
"node_modules",
"**/*.spec.ts",
]
}
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"sourceMap": true,
"removeComments": true,
"preserveConstEnums": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}

0 comments on commit fef4e48

Please sign in to comment.