Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
- DiscordBot, TwitchBot, Server into one logic
- Emitter setup
  • Loading branch information
ma7dev committed Dec 15, 2021
1 parent f4c7847 commit 8dd42bf
Show file tree
Hide file tree
Showing 59 changed files with 504 additions and 702 deletions.
104 changes: 104 additions & 0 deletions src/DiscordBot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require('dotenv').config({path:'../.env'});

const fs = require('fs'),
discord = require("discord.js"),
{ Collection, Intents } = require('discord.js'),
{ promisify } = require('util');

class DiscordBot {
constructor() {
this.guild = null;
this.twitchBadge = null;

this.client = new discord.Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_BANS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_INTEGRATIONS,
Intents.FLAGS.GUILD_WEBHOOKS,
Intents.FLAGS.GUILD_INVITES,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING,
]
});


// commands
this.client.commands = new Collection();

const commandFiles = fs.readdirSync("./discord/commands").filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = require(`./discord/commands/${file}`);
this.client.commands.set(command.data.name, command);
}

// events
const eventFiles = fs.readdirSync('./discord/events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const event = require(`./discord/events/${file}`);
if (event.once) {
this.client.once(event.name, (...args) => event.execute(this.client,...args));
} else {
this.client.on(event.name, (...args) => event.execute(this.client,...args));
}
}
}
start() {
this.client.login(process.env.DISCORD_TOKEN)
.catch(console.error);
}
restart() {
console.log('restarting...')
this.client.destroy()
.then(() => {
this.start();
});
}
// update stream-notification channel
updateStreamNotificationChannel(channel) {

}
sendMessage(channelId, message) {
const channel = this.guild.channels.cache.get(channelId);
channel.send(message);

}
liveNotification(channelId, streamer) {
const channel = this.guild.channels.cache.get(channelId),
message = `Hey @everyone, <@${streamer.discordId}> has gone live on Twitch Watch them here: \n`
+ `https://www.twitch.tv/${streamer.twitchName}`;
channel.send(message);

}
liveEmbed(rank, streamers) {
const channel = this.guild.channels.cache.get(rank.channelId);
if(!rank.messageId) {
const embed = generateEmbed(rank,streamers);
channel.send(embed);
rank.messageId = embed.id;
}
else {
rank.messageId.edit(generateEmbed(rank,streamers))
}
// check if bot sent a message before
// if yes, update it
// else, create a new one
}
// update stats channels
// update featured
// update levels
// backup
// logs
}

module.exports = { DiscordBot };
77 changes: 77 additions & 0 deletions src/TwitchBot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require('dotenv').config({path:'../.env'});

const fs = require('fs'),
tmi = require('tmi.js'),
{ Collection } = require('discord.js');

class TwitchBot {
constructor(channels) {
console.log(channels)
this.client = new tmi.Client({
options: {
debug: true,
messagesLogLevel: "info"
},
connection: {
reconnect: true,
secure: true
},
identity: {
username: `${process.env.TWITCH_BOT_USERNAME}`,
password: `oauth:${process.env.TWITCH_ACCESS_TOKEN}`
},
channels
});

// commands
this.client.commands = new Collection();

const commandFiles = fs.readdirSync("./twitch/commands").filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = require(`./twitch/commands/${file}`);
this.client.commands.set(command.name, command);
}

// events
const eventFiles = fs.readdirSync('./twitch/events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const event = require(`./twitch/events/${file}`);
if (event.once) {
this.client.once(event.name, (...args) => event.execute(this.client,...args));
} else {
this.client.on(event.name, (...args) => event.execute(this.client,...args));
}
}
}
start() {
console.log('starting...')
this.client.connect().catch(err => console.log(err));
}
disconnect() {
console.log('disconnecting...')
this.client.disconnect()
.then((data) => {
// data returns [server, port]
}).catch(err => console.log(err));
}
restart(){
console.log('restarting...')
this.client.disconnect().catch(err => console.log(err));
this.start();
}
join(channel) {
this.client.join(channel).catch(err => console.log(err));
}
leave(channel) {
this.client.part(channel).catch(err => console.log(err));
}
host(channel, target) {
this.client.host(channel, target).catch(err => console.log(err));
}
// TODO: to verify a user, send a message to whisper

}

module.exports = { TwitchBot };
2 changes: 1 addition & 1 deletion src/ai/ArcaneGAN/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def process(output_path,mtcnn,model,img):
args = parser.parse_args()

project_path = "/home/alotaima/Projects/side/onlysudo/src/ai/ArcaneGAN"
args.outdir = '/src/api/public/ai/arcane'
args.outdir = '/src/server/public/ai/arcane'
args.size = 1024

if args.url == '':
Expand Down
4 changes: 2 additions & 2 deletions src/ai/BlendGAN/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
args.ckpt = f'{project_path}/pretrained_models/blendgan.pt'
args.psp_encoder_ckpt = f'{project_path}/pretrained_models/psp_encoder.pt'
args.style_img_path = f'{project_path}/test_imgs/style_imgs/'
args.add_weight_index = 6
args.outdir = '/src/api/public/ai/style_transfer'
args.add_weight_index = 10
args.outdir = '/src/server/public/ai/style_transfer'
args.channel_multiplier = 2
args.latent = 512
args.n_mlp = 8
Expand Down
2 changes: 1 addition & 1 deletion src/ai/anime/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
args = parser.parse_args()

project_path = "/home/alotaima/Projects/side/onlysudo/src/ai/anime"
args.outdir = '/src/api/public/ai/anime'
args.outdir = '/src/server/public/ai/anime'

root_path = '/'.join(os.path.abspath(os.getcwd()).split('/')[:-2])
output_path = f"{root_path}{args.outdir}/{args.filename}"
Expand Down
2 changes: 1 addition & 1 deletion src/ai/pixel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
args = parser.parse_args()

project_path = "/home/alotaima/Projects/side/onlysudo/src/ai/pyxelate"
args.outdir = '/src/api/public/ai/pixel'
args.outdir = '/src/server/public/ai/pixel'
# args.outdir = '/api/public/ai/pixel'

root_path = '/'.join(os.path.abspath(os.getcwd()).split('/')[:-2])
Expand Down
43 changes: 0 additions & 43 deletions src/api/index.js

This file was deleted.

19 changes: 0 additions & 19 deletions src/api/models/Rank.js

This file was deleted.

57 changes: 0 additions & 57 deletions src/api/models/Streamer.js

This file was deleted.

Loading

0 comments on commit 8dd42bf

Please sign in to comment.