Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove twitch #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ COPY index.js index.js

ENV NODE_ENV=production

ENTRYPOINT ["node", "/usr/src/app/index.js", "--chat=dgg"]
ENTRYPOINT ["node", "/usr/src/app/index.js"]
CMD ["node", "/usr/src/app/index.js"]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
You'll need a configuration file, there's a sample configuration under ./configuration, rename this to prod.config.json
and enter in your own keys to hit the APIs. Documentation on configuration coming eventually.

You shouldn't need api keys for most commands, but you will definitely need them to run the bot against twitch.
You shouldn't need api keys for most commands.

See below for local development of the bot.

Expand Down Expand Up @@ -54,11 +54,11 @@ Durations are in the format of the number, followed by h,m,s or d.

500d

(Note on adding commands/scheduled commands for admins, when you add the commands to twitch/dgg, you need to !restart the bot in the other chat to pick these changes up. This will change with a future update.)
(Note on adding commands/scheduled commands for admins, when you add the commands to dgg, you need to !restart the bot in the other chat to pick these changes up. This will change with a future update.)

| Command | Input | What it does | Requires Admin | Example |
|------------------------------------------------|------------------------------|---------------------------------------------------------------------------------------------------|----------------|-----------------------------------------------------------|
| !restart | None | Restarts the dgg or Twitch.tv bot. Don't overuse the command, it resets caches. | Yes | !restart |
| !restart | None | Restarts the dgg bot. Don't overuse the command, it resets caches. | Yes | !restart |
| !stalk | {user} (postnumber) | Overrustle logs posts | Yes | !stalk Destiny 5 |
| !time | None | Current Steven Time | No | !time |
| !schedule !sch | None | Show next upcoming scheduled event, and a link to the schedule calendar. | No | !schedule |
Expand Down
18 changes: 2 additions & 16 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,8 @@ services:
volumes:
- ./prod.config.json:/usr/src/app/lib/configuration/prod.config.json:ro
- ./database:/usr/src/app/database
command: ["node", "/usr/src/app/index.js", "--chat=dgg"]
command: ["node", "/usr/src/app/index.js"]
restart: unless-stopped
network_mode: "host"
environment:
- NODE_ENV=production
bot-ttv:
build:
context: .
dockerfile: Dockerfile
container_name: bot-ttv
user: 1004:1004
volumes:
- ./prod.config.json:/usr/src/app/lib/configuration/prod.config.json:ro
- ./database:/usr/src/app/database
command: ["node", "/usr/src/app/index.js", "--chat=twitch"]
restart: unless-stopped
network_mode: "host"
environment:
- NODE_ENV=production
- NODE_ENV=production
32 changes: 11 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const { argv } = require('yargs');

const DestinyChat = require('./lib/services/destinychat');
const TwitchChat = require('./lib/services/twitch-chat');
const CommandRouter = require('./lib/message-routing/command-router');
const Services = require('./lib/services/service-index');
const loadConfig = require('./lib/configuration/config-loader');
Expand All @@ -15,22 +14,20 @@ const {
const { configureReporter } = require('./lib/services/metrics/metrics-reporter');

const config = loadConfig(argv.config);
const chatToConnectTo = argv.chat || config.chatToConnectTo;
config.chatToConnectTo = chatToConnectTo;

if (config === null) {
// eslint-disable-next-line no-console
console.log('WARNING: Config file not found, no config loaded. Shutting down.');
process.exit(0);
}
const services = new Services(config, chatToConnectTo);
configureReporter(config.influx, new Map([['chat', config.chatToConnectTo]]));

const services = new Services(config);
configureReporter(config.influx, new Map([['chat', 'dgg']]));
const { logger } = services;

services
.prepareAsyncServices()
.then(() => {
registerCommandsFromFiles(services.commandRegistry, chatToConnectTo, config);
registerCommandsFromFiles(services.commandRegistry, config);
logger.info('Config loaded! Starting bot!');
return setupCommandsAndCachesFromDb(
services.sql,
Expand All @@ -43,25 +40,18 @@ services
});
})
.then(() => {
logger.info(`Configuring for ${chatToConnectTo} chat`);
logger.info(`Configuring for dgg chat`);
const commandRouter = new CommandRouter(services);
const messageRouter = new MessageRouter({ chatConnectedTo: chatToConnectTo }, services);
let bot = null;
const messageRouter = new MessageRouter(services);
const bot = new DestinyChat(config.dggChat, services);

if (chatToConnectTo === 'twitch') {
bot = new TwitchChat(config.twitch, services);
} else if (chatToConnectTo === 'dgg') {
bot = new DestinyChat(config.dggChat, services);
} else {
logger.error('Config property: "chatToConnectTo" not set to one of "dgg" or "twitch"');
process.exit(1);
}
if(config.hasOwnProperty('scheduledCommands')){
config.scheduledCommands.forEach(commandToSchedule => services.fakeScheduler.createMessage(commandToSchedule));
if (config.hasOwnProperty('scheduledCommands')) {
config.scheduledCommands.forEach((commandToSchedule) =>
services.fakeScheduler.createMessage(commandToSchedule),
);
}

const chatServiceRouter = new ChatServiceRouter(
config.chatToConnectTo,
bot,
messageRouter,
commandRouter,
Expand Down
67 changes: 1 addition & 66 deletions lib/commands/implementations/banphrase.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,10 @@
const _ = require('lodash');
const Command = require('../command-interface');
const CommandOutput = require('../command-output');
const parseDurationToSeconds = require('../../chat-utils/duration-parser');

function banPhraseTwitch(defaultPunishmentDuration, punishmentType) {
return (input, services) => {
const matched = /(\d+[HMDSWwhmds])?\s?(.*)/.exec(input);
const duration = _.get(matched, 1, '');
const bannedPhrase = _.get(matched, 2, '').toLowerCase();
let parsedDuration = defaultPunishmentDuration;

if (duration !== '') {
parsedDuration = parseDurationToSeconds(duration);
if (parsedDuration === null) {
return Promise.resolve(
new CommandOutput(
null,
'Could not parse the duration. Usage: "!AddX {amount}{m,h,d,w} {some banned phrase} " !AddMute 1d YOU BEEN GNOMED',
),
);
}
}

if (/^\/.*\/$/.test(bannedPhrase)) {
try {
// eslint-disable-next-line no-new
new RegExp(bannedPhrase);
} catch (e) {
return Promise.resolve(new CommandOutput(null, 'Could not add phrase. Invalid Regex.'));
}
}

return services.sql
.addBannedPhrase(bannedPhrase, parsedDuration, punishmentType)
.then(() => {
services.spamDetection.addBannedPhrase({
text: bannedPhrase,
duration: parsedDuration,
type: punishmentType,
});
return new CommandOutput(null, 'Phrase banned!');
})
.catch((err) => {
if (err.errno === 19) {
return new CommandOutput(null, 'Phrase already banned!');
}
return new CommandOutput(err, 'Oops. Something did not work. Check the logs.');
});
};
}

function banPhraseDGG() {
return () => {
return new CommandOutput(null, 'This command has been removed, use native /addban instead.');
};
}

module.exports = {
addbanTwitch: new Command(
banPhraseTwitch(1800, 'ban'),
true,
true,
/(\d+[HMDSWwhmds])?\s?(.*)/,
false,
),
addmuteTwitch: new Command(
banPhraseTwitch(600, 'mute'),
true,
true,
/(\d+[HMDSWwhmds])?\s?(.*)/,
false,
),
addbanDGG: new Command(banPhraseDGG(), false, true, /(\d+[HMDSWwhmds])?\s?(.*)/, false),
};
module.exports = new Command(banPhraseDGG(), false, true, /(\d+[HMDSWwhmds])?\s?(.*)/, false);
23 changes: 1 addition & 22 deletions lib/commands/implementations/unbanphrase.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
const _ = require('lodash');
const Command = require('../command-interface');
const CommandOutput = require('../command-output');

function unbanPhraseTwitch() {
return (input, services) => {
const matched = /(.*)/.exec(input);
const phraseToUnban = _.get(matched, 1, '').toLowerCase();
if (services.spamDetection.hasBannedPhrase(phraseToUnban) === false) {
return Promise.resolve(new CommandOutput(null, 'Phrase is not registered! Did nothing.'));
}
return services.sql
.deleteBannedPhrase(phraseToUnban)
.then(() => {
services.spamDetection.removeBannedPhrase(phraseToUnban);
return new CommandOutput(null, 'Phrase unbanned! AngelThump');
})
.catch((err) => new CommandOutput(err, 'Oops. Something did not work. Check the logs.'));
};
}

function unbanPhraseDGG() {
return () => {
return new CommandOutput(null, 'This command has been removed, use native /removeban instead.');
};
}

module.exports = {
unbanphraseTwitch: new Command(unbanPhraseTwitch(), true, true, /(.*)/, false),
unbanphraseDGG: new Command(unbanPhraseDGG(), false, true),
};
module.exports = new Command(unbanPhraseDGG(), false, true);
75 changes: 30 additions & 45 deletions lib/configuration/configure-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const song = require('../commands/implementations/song');
const earlierSong = require('../commands/implementations/earliersong');
const youtube = require('../commands/implementations/youtube');
const schedule = require('../commands/implementations/schedule');
const { addbanDGG, addbanTwitch, addmuteTwitch } = require('../commands/implementations/banphrase');
const { unbanphraseDGG, unbanphraseTwitch } = require('../commands/implementations/unbanphrase');
const addbanDGG = require('../commands/implementations/banphrase');
const unbanphraseDGG = require('../commands/implementations/unbanphrase');
const live = require('../commands/implementations/live');
const restart = require('../commands/implementations/restart');
const love = require('../commands/implementations/love');
Expand All @@ -33,7 +33,7 @@ const { gulag } = require('../commands/implementations/gulag');
const { mutelinks } = require('../commands/implementations/mutelinks');
const { breakingNews } = require('../commands/implementations/breaking-news');

function registerCommandsFromFiles(commandRegistry, chatConnectedTo, config) {
function registerCommandsFromFiles(commandRegistry, config) {
commandRegistry.registerCommand('!stalk', stalk);
commandRegistry.registerCommand('!time', time(config.timezone, config.timezoneString));
commandRegistry.registerCommand('!addcommand', addCommand, ['!ac']);
Expand Down Expand Up @@ -80,48 +80,33 @@ function registerCommandsFromFiles(commandRegistry, chatConnectedTo, config) {
'!linksmute',
]);
commandRegistry.registerCommand('!breakingnews', breakingNews, ['!breaking', '!bn']);

if (chatConnectedTo === 'dgg') {
commandRegistry.registerCommand('!addban', addbanDGG, ['!addmute']);
commandRegistry.registerCommand('!deleteban', unbanphraseDGG, [
'!deletemute',
'!removeban',
'!removemute',
'!dmute',
'!dban',
]);
commandRegistry.registerCommand('!voteban', voteBan);
commandRegistry.registerCommand('!voteipban', voteIpban);
commandRegistry.registerCommand('!svoteban', svoteBan);
commandRegistry.registerCommand('!svoteipban', svoteIpban);
commandRegistry.registerCommand('!questions', getThreadInfo, [
'!podcastquestions',
'!dtquestions',
'!dtsubmissions',
'!!dtq',
]);
commandRegistry.registerCommand('!newsubquestions', startNewThread, [
'!nsq',
'!newredditquestions',
]);
commandRegistry.registerCommand('!stopsubquestions', stopQuestionSubmissions, [
'!ssq',
'!stopredditquestions',
]);
commandRegistry.registerCommand('!gulag', gulag);
}

if (chatConnectedTo === 'twitch') {
commandRegistry.registerCommand('!addban', addbanTwitch);
commandRegistry.registerCommand('!addmute', addmuteTwitch);
commandRegistry.registerCommand('!deleteban', unbanphraseTwitch, [
'!deletemute',
'!removeban',
'!removemute',
'!dmute',
'!dban',
]);
}
commandRegistry.registerCommand('!addban', addbanDGG, ['!addmute']);
commandRegistry.registerCommand('!deleteban', unbanphraseDGG, [
'!deletemute',
'!removeban',
'!removemute',
'!dmute',
'!dban',
]);
commandRegistry.registerCommand('!voteban', voteBan);
commandRegistry.registerCommand('!voteipban', voteIpban);
commandRegistry.registerCommand('!svoteban', svoteBan);
commandRegistry.registerCommand('!svoteipban', svoteIpban);
commandRegistry.registerCommand('!questions', getThreadInfo, [
'!podcastquestions',
'!dtquestions',
'!dtsubmissions',
'!!dtq',
]);
commandRegistry.registerCommand('!newsubquestions', startNewThread, [
'!nsq',
'!newredditquestions',
]);
commandRegistry.registerCommand('!stopsubquestions', stopQuestionSubmissions, [
'!ssq',
'!stopredditquestions',
]);
commandRegistry.registerCommand('!gulag', gulag);
}

async function setupCommandsAndCachesFromDb(
Expand Down
14 changes: 5 additions & 9 deletions lib/configuration/sample.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"chatToConnectTo": "dgg",
"timezone": "America/Tijuana",
"timezoneString": "svensk",
"dggChat": {
Expand All @@ -8,7 +7,10 @@
"cookieToken": "yourCookieToken",
"botNick": "yourBotNick"
},
"scheduledCommands": ["!youtube", "!schedule"],
"scheduledCommands": [
"!youtube",
"!schedule"
],
"logger": {
"level": "debug"
},
Expand Down Expand Up @@ -86,12 +88,6 @@
"schedule": {
"messageIntervalMillis": 900000
},
"twitch": {
"clientId": "yourClientId",
"clientSecret": "yourClientSecret",
"accessToken": "yourAccesstoken",
"channelId": "yourChannel"
},
"twitter": {
"consumerKey": "",
"consumerSecret": "",
Expand Down Expand Up @@ -134,4 +130,4 @@
"threadFilePath": "",
"stateStoreFilePath": ""
}
}
}
Loading