Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandons404 committed Sep 30, 2022
0 parents commit 64f3f13
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Rock The Vote

A lightweight javascript re-write of the original [RockTheVotePlugin](https://github.com/mayli/RockTheVotePlugin) updated to run on V7 servers.

## Installing

Simply download the zip file, pull the folder inside of it out, and put it into your config/mods folder.
(A server restart is required to update mods/plugins on your server.)

## Disclaimer

I am not claiming ownership of this plugin, original credit goes to [mayli](https://github.com/mayli) who made it first. It doesn't seem like it is being supported anymore and I rely on it for my own server, so I decided to update it myself.

There were some listed Todo's on the original plugin that I may get to later since I think they would be nice features, or if you want, feel free to create a pull request on this repo.

Please create a new issue if you find any bugs, I'm happy to try and fix them.
7 changes: 7 additions & 0 deletions mod.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: easyDiscordPlugin
displayName: easyDiscordPlugin
author: >;;;>Fish
description: "2 way communication between discord and game chat."
version: 1.0
minGameVersion: 136
hidden: true
11 changes: 11 additions & 0 deletions scripts/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const logg = (msg) => Call.sendMessage(msg);
const list = (ar) => Call.sendMessage(ar.join(' | '));
const keys = (obj) => Call.sendMessage(Object.keys(obj).join(' [scarlet]|[white] '));
const info = (msg) => Call.infoMessage(msg);

module.exports = {
log: logg,
list: list,
keys: keys,
info: info,
};
137 changes: 137 additions & 0 deletions scripts/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
const utils = require('helper');
importPackage(Packages.arc.util);

// for testing locally
// const ip = 'localhost';
const ip = '45.79.202.111';

const discordPrefix = '';
let channelId = '';
let serverCommands;

const sendMessage = (msg) => {
const postBody = {
channel: channelId,
msg: msg,
};

const stringPostBody = JSON.stringify(postBody);

const req = Http.post(`http://` + ip + `:5000/api/chat`, stringPostBody)
.header('Content-Type', 'application/json')
.header('Accept', '*/*');
req.timeout = 10000;

try {
req.submit((response, exception) => {
if (exception || !response) {
Log.info(
'\n\nDiscord bot encountered an error while trying to send a message to discord.\n\n'
);
}
return;
});
} catch (e) {
Log.info('\n\nDiscord bot encountered an error while trying to send a message to discord.\n\n');
}
};

Events.on(PlayerJoin, (e) => {
const player = e.player;
const formattedName = Strings.stripColors(player.name);
const msg = '**' + formattedName + ' Joined.' + '**';

sendMessage(msg);
});

Events.on(PlayerLeave, (e) => {
const player = e.player;
const formattedName = Strings.stripColors(player.name);
const msg = '**' + formattedName + ' Left.' + '**';

sendMessage(msg);
});

Events.on(PlayerChatEvent, (e) => {
const player = e.player;
const text = e.message;

if (text[0] === '/') return;

const formattedName = Strings.stripColors(player.name);
let formattedMessage = text.slice(0, -2);
const lastChar1 = formattedMessage[formattedMessage.length - 1];

if (lastChar1 >= 0xf80 && lastChar1 <= 0x107f) {
formattedMessage = formattedMessage.slice(0, -2);
}

const msg = '**' + formattedName + '**' + ': ' + formattedMessage;

sendMessage(msg);
});

Events.on(ServerLoadEvent, (e) => {
serverCommands = Core.app.listeners.find(
(l) => l instanceof Packages.mindustry.server.ServerControl
).handler;

const runner = (method) => new Packages.arc.util.CommandHandler.CommandRunner({ accept: method });

const savedChannelId = Core.settings.get('discordChatBot', '');

if (savedChannelId === '') {
Log.info(
'\n\nDiscord Bot: No discord channel was found. Please use "setchannel <channelId> to set it!\n\n'
);
}

if (savedChannelId !== '') {
channelId = savedChannelId;
}

// setChannel
serverCommands.register(
'setchannel',
'<channelId>',
'set the discord channel id to sync with.',
runner((args) => {
channelId = args[0];

Core.settings.put('discordChatBot', channelId);
Core.settings.manualSave();
Log.info('Discord channel id set to: ' + channelId);
return;
})
);
});

Timer.schedule(
() => {
if (!channelId) return;

const postBody = {
channelId: channelId,
};

const stringPostBody = JSON.stringify(postBody);

const req = Http.post(`http://` + ip + `:5000/api/discord`, stringPostBody)
.header('Content-Type', 'application/json')
.header('Accept', '*/*');
req.timeout = 10000;

try {
req.submit((response, exception) => {
if (exception || !response) return;
let messages = response.getResultAsString();
messages = JSON.parse(messages).messages;
if (messages.length > 0) Call.sendMessage(messages);
});
} catch (e) {
Log.info('\n\nDiscord Bot: There was a problem getting discord messages.\n\n');
}
},
10,
3
);

0 comments on commit 64f3f13

Please sign in to comment.