Browse Source

started letters

main
Chris Mahoney 2 years ago
parent
commit
d5db3c22b7
  1. 2
      .dockerignore
  2. 4
      .gitignore
  3. 8
      .sequelizerc
  4. 116
      README.md
  5. 23
      config.js
  6. 0
      database.sqlite
  7. BIN
      database.sqlite3
  8. 10
      db/models/index.ts
  9. 29
      db/models/user.ts
  10. 8
      docker-compose.yaml
  11. 29
      dockerfile
  12. 1783
      package-lock.json
  13. 44
      package.json
  14. 17
      src/letters.ts
  15. 68
      src/main.ts
  16. 195
      src/rules.ts
  17. 18
      tsconfig.json

2
.dockerignore

@ -1,2 +1,2 @@
node_modules/
node_modules/
dist/

4
.gitignore

@ -1,3 +1,3 @@
node_modules/
dist/
node_modules/
dist/
.env

8
.sequelizerc

@ -0,0 +1,8 @@
const path = require('path');
module.exports = {
config: path.resolve('.', 'config.js'),
'models-path': path.resolve('./db/models'),
'seeders-path': path.resolve('./db/seeders'),
'migrations-path': path.resolve('./db/migrations')
};

116
README.md

@ -1,59 +1,59 @@
# EnforcerBot2
Discord bot that enforces the rules on my server. The bot is currently running on dubsdot2.
## Running
Clone the repo
```
git clone https://gitea.cslabs.clarkson.edu/mahonec/EnforcerBot2.git
```
Ignore the error and checkout the `main` branch
```
cd EnforcerBot2
git checkout main
```
Create a `.env` file and add your bot's api token like so:
```
TOKEN=<copy and paste the token>
```
Finally as **root** bring the service up with [docker-compose](https://docs.docker.com/compose/install/)
```
sudo docker-compose up -d
```
## Adding rules
Rules are relatively straight forward to add. In [rules.ts](/src/rules.ts) there is a `Map` named rules that maps `channel.id` strings to callback functions; like so:
```ts
var rules : Map<string, (msg: Discord.Message) => void> = new Map();
```
So adding a new rule is as simple as:
```ts
// # no posting whatsoever
rules.set("826181775981019156", function(msg) {
msg.delete();
});
```
Now whenever a message is posted or edited [index.ts](/src/index.ts) will check if `msg.channel.id` has an associated function and, if so, it will call that with `msg`:
```ts
// get the rule
let rule : ((msg: Discord.Message) => void) | undefined = rules.get(msg.channel.id);
// check if the rule exists
if (rule != undefined) {
// callback
rule(msg);
}
# EnforcerBot2
Discord bot that enforces the rules on my server. The bot is currently running on dubsdot2.
## Running
Clone the repo
```
git clone https://gitea.cslabs.clarkson.edu/mahonec/EnforcerBot2.git
```
Ignore the error and checkout the `main` branch
```
cd EnforcerBot2
git checkout main
```
Create a `.env` file and add your bot's api token like so:
```
TOKEN=<copy and paste the token>
```
Finally as **root** bring the service up with [docker-compose](https://docs.docker.com/compose/install/)
```
sudo docker-compose up -d
```
## Adding rules
Rules are relatively straight forward to add. In [rules.ts](/src/rules.ts) there is a `Map` named rules that maps `channel.id` strings to callback functions; like so:
```ts
var rules : Map<string, (msg: Discord.Message) => void> = new Map();
```
So adding a new rule is as simple as:
```ts
// # no posting whatsoever
rules.set("826181775981019156", function(msg) {
msg.delete();
});
```
Now whenever a message is posted or edited [index.ts](/src/index.ts) will check if `msg.channel.id` has an associated function and, if so, it will call that with `msg`:
```ts
// get the rule
let rule : ((msg: Discord.Message) => void) | undefined = rules.get(msg.channel.id);
// check if the rule exists
if (rule != undefined) {
// callback
rule(msg);
}
```

23
config.js

@ -0,0 +1,23 @@
{
"development": {
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}

0
database.sqlite

BIN
database.sqlite3

10
db/models/index.ts

@ -0,0 +1,10 @@
import { Sequelize } from 'sequelize';
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../../config.js')[env];
const sequelize = config.url
? new Sequelize(config.url, config)
: new Sequelize(config.database, config.username, config.password, config);
export { Sequelize, sequelize };

29
db/models/user.ts

@ -0,0 +1,29 @@
// /models/User.ts
import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from './index';
interface UserAttributes {
id: string;
}
interface UserCreationAttributes
extends Optional<UserAttributes, 'id'> {}
interface UserInstance
extends Model<UserAttributes, UserCreationAttributes>,
UserAttributes {
createdAt?: Date;
updatedAt?: Date;
}
const User = sequelize.define<UserInstance>(
'User',
{
id: {
primaryKey: true,
type: DataTypes.STRING,
},
}
);
export default User;

8
docker-compose.yaml

@ -1,4 +1,4 @@
services:
bot:
build: .
env_file: .env
services:
bot:
build: .
env_file: .env

29
dockerfile

@ -1,14 +1,17 @@
FROM node:14-alpine
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
COPY package*.json ./
RUN npm install
# Compile project
COPY . .
RUN npm run build
FROM node:14-alpine
# Add sqlite package
RUN apk add sqlite
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
COPY package*.json ./
RUN npm install
# Compile project
COPY . .
RUN npm run build
CMD [ "node", "dist/index.js" ]

1783
package-lock.json
File diff suppressed because it is too large
View File

44
package.json

@ -1,21 +1,23 @@
{
"name": "enforcerbot2",
"version": "1.1.0",
"description": "",
"main": "index.js",
"dependencies": {
"discord.js": "^12.5.1",
"dotenv": "^8.2.0"
},
"scripts": {
"build": "tsc",
"start": "npm run build && node dist/index.js"
},
"devDependencies": {
"@types/node": "^14.14.37",
"@types/ws": "^7.4.0",
"typescript": "^4.2.3"
},
"author": "",
"license": "ISC"
}
{
"name": "enforcerbot2",
"version": "1.2.0",
"description": "",
"main": "main.js",
"dependencies": {
"discord.js": "^12.5.1",
"dotenv": "^8.6.0",
"sequelize": "^6.6.2",
"sqlite3": "^5.0.2"
},
"scripts": {
"build": "tsc",
"start": "npm run build && node dist/main.js"
},
"devDependencies": {
"@types/node": "^14.14.45",
"@types/ws": "^7.4.4",
"typescript": "^4.2.3"
},
"author": "",
"license": "ISC"
}

17
src/letters.ts

@ -0,0 +1,17 @@
// Listens in #1-letter-per-day and posts/edits messages in #letters
import Discord = require('discord.js');
var letters_messages = function(msg: Discord.Message | Discord.PartialMessage) {
}
var letters_reactions = function(reaction: Discord.MessageReaction, user: Discord.User | Discord.PartialUser) {
// Require reactions in #letters
if (reaction.message.channel.id != "834757828999643157")
return;
console.log(reaction);
}
module.exports = [letters_messages, letters_reactions];

68
src/index.ts → src/main.ts

@ -1,32 +1,36 @@
require('dotenv').config();
import Discord = require('discord.js');
var rules : Map<string, (msg: Discord.Message | Discord.PartialMessage) => void> = require('./rules');
const bot = new Discord.Client();
bot.on('ready', () => {
console.info(`Logged in as ${bot.user!.tag}!`);
});
bot.on('message', (msg) => {
// Exclude itself
if (msg.author.id == "826495787240390727") return;
let rule = rules.get(msg.channel.id);
if (rule != undefined) {
rule(msg);
}
})
bot.on('messageUpdate', (_, newmsg) => {
// Exclude itself
if (newmsg.author?.id == "826495787240390727") return;
let rule = rules.get(newmsg.channel.id);
if (rule != undefined) {
rule(newmsg);
}
})
bot.login(process.env.TOKEN);
require('dotenv').config();
import Discord = require('discord.js');
var handle_rules = require('./rules');
var [letters_messages, letters_reactions] = require('./letters');
import User = require('./db');
const bot = new Discord.Client();
bot.on('ready', () => {
console.info(`Logged in as ${bot.user!.tag}!`);
});
bot.on('message', (msg) => {
// Exclude itself
if (msg.author.id == "826495787240390727") return;
handle_rules(msg);
letters_messages(msg);
});
bot.on('messageUpdate', (_, newmsg) => {
// Exclude itself
if (newmsg.author?.id == "826495787240390727") return;
handle_rules(newmsg);
letters_messages(newmsg);
});
bot.on('messageReactionAdd', (reaction, user) => {
letters_reactions(reaction, user);
});
bot.login(process.env.TOKEN);

195
src/rules.ts

@ -1,94 +1,101 @@
import Discord = require('discord.js');
var rules : Map<string, (msg: Discord.Message | Discord.PartialMessage) => void> = new Map();
// # avoid-5
rules.set("826868893905322024", function(msg) {
if (!msg.content) {
return;
}
const banned = [ 'ℇ', 'e', 'E', '℈', '℮', 'ℯ', 'ℰ', 'ⅇ', '🇪', 'Ə' ]
function bold(msg: string) {
for (let i = 0; i < banned.length; i++) {
let str = banned[i];
msg = msg.replace(str, "**" + str + "**");
}
return msg;
}
for (let i = 0; i < banned.length; i++) {
let str = banned[i];
console.log(str, msg.content.includes(str));
if (msg.content.includes(str)) {
msg.author?.send("Your message was cringe and included a banned character:\n> " + bold(msg.content));
msg.delete();
break;
}
}
});
// # embrace-e
rules.set("826868953761710140", function(msg) {
if (!msg.content) {
return;
}
let words = msg.content.split(/[\s]+/);
for (let i = 0; i < words.length; i++) {
let word = words[i];
if (Discord.MessageMentions.CHANNELS_PATTERN.test(word) || Discord.MessageMentions.EVERYONE_PATTERN.test(word)
|| Discord.MessageMentions.ROLES_PATTERN.test(word) || Discord.MessageMentions.USERS_PATTERN.test(word)) {
continue;
}
if (!word.includes('e') && !word.includes('E') ) {
msg.author?.send("You forget an e in this word: `" + word + "` in this post:\n> " + msg.content);
msg.delete();
return;
}
}
});
// # no posting whatsoever
rules.set("826869062541639682", function(msg) {
msg.delete();
});
// # bot spam
rules.set("826868377024593930", function(msg) {
if (msg.content != "bot") {
msg.delete();
}
if (Math.random() < 0.20) {
setTimeout(() => msg.channel.send("bot"), 7000);
}
});
// # odd
rules.set("834816504939675688", function(msg) {
let discriminator = parseInt(msg.author!.discriminator);
// yeet even users
if (discriminator % 2 == 0) {
msg.delete();
}
});
// # even
rules.set("834816520856666172", function(msg) {
let discriminator = parseInt(msg.author!.discriminator);
// yeet odd users
if (discriminator % 2 == 1) {
msg.delete();
}
});
module.exports = rules;
import Discord = require('discord.js');
var rules : Map<string, (msg: Discord.Message | Discord.PartialMessage) => void> = new Map();
function handle_rules(msg: Discord.Message | Discord.PartialMessage) {
let rule = rules.get(msg.channel.id);
if (rule != undefined) {
rule(msg);
}
}
// # avoid-5
rules.set("826868893905322024", function(msg) {
if (!msg.content) {
return;
}
const banned = [ 'ℇ', 'e', 'E', '℈', '℮', 'ℯ', 'ℰ', 'ⅇ', '🇪', 'Ə' ]
function bold(msg: string) {
for (let i = 0; i < banned.length; i++) {
let str = banned[i];
msg = msg.replace(str, "**" + str + "**");
}
return msg;
}
for (let i = 0; i < banned.length; i++) {
let str = banned[i];
console.log(str, msg.content.includes(str));
if (msg.content.includes(str)) {
msg.author?.send("Your message was cringe and included a banned character:\n> " + bold(msg.content));
msg.delete();
break;
}
}
});
// # embrace-e
rules.set("826868953761710140", function(msg) {
if (!msg.content) {
return;
}
let words = msg.content.split(/[\s]+/);
for (let i = 0; i < words.length; i++) {
let word = words[i];
if (Discord.MessageMentions.CHANNELS_PATTERN.test(word) || Discord.MessageMentions.EVERYONE_PATTERN.test(word)
|| Discord.MessageMentions.ROLES_PATTERN.test(word) || Discord.MessageMentions.USERS_PATTERN.test(word)) {
continue;
}
if (!word.includes('e') && !word.includes('E') ) {
msg.author?.send("You forget an e in this word: `" + word + "` in this post:\n> " + msg.content);
msg.delete();
return;
}
}
});
// # no posting whatsoever
rules.set("826869062541639682", function(msg) {
msg.delete();
});
// # bot spam
rules.set("826868377024593930", function(msg) {
if (msg.content != "bot") {
msg.delete();
}
if (Math.random() < 0.20) {
setTimeout(() => msg.channel.send("bot"), 7000);
}
});
// # odd
rules.set("834816504939675688", function(msg) {
let discriminator = parseInt(msg.author!.discriminator);
// yeet even users
if (discriminator % 2 == 0) {
msg.delete();
}
});
// # even
rules.set("834816520856666172", function(msg) {
let discriminator = parseInt(msg.author!.discriminator);
// yeet odd users
if (discriminator % 2 == 1) {
msg.delete();
}
});
module.exports = handle_rules;

18
tsconfig.json

@ -1,10 +1,10 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"sourceRoot": "src",
"outDir": "dist",
"sourceMap": true
}
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"sourceRoot": "src",
"outDir": "dist",
"sourceMap": true
}
}
Loading…
Cancel
Save