forked from steaksweremade/Transferbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
115 lines (100 loc) · 4.48 KB
/
bot.js
File metadata and controls
115 lines (100 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Niklas Leinz
//--------------
// Restful API (Uncomment this area to have a Restful API)
/*
const http = require('http');
const app = require('./app');
const port = process.env.PORT || 3000; // change 3000 to Costum port
const server = http.createServer(app);
server.listen(port);
*/
// Init winston logger (logger.js)
var penis = require(`${__dirname}/logger.js`);
var logger = penis.logger;
var methods = require(`${__dirname}/outsource.js`);
//------------------------
// GETTING THE BOT RUNNING
//------------------------
// Init Telegram Bot
const Telegraf = require('telegraf');
const bot = new Telegraf(methods.getToken('telegram'));
bot.startPolling(); //executes telegram bot
bot.start((ctx) => ctx.reply('Welcome')); //Bot first Join
// Init Discord Bot
const Discord = require('discord.js');
const client = new Discord.Client();
client.login(methods.getToken("discord")); //login into discord bot (Token)
//-------------------------------------
// Bot started
console.log("-------------------------");
logger.log("info", "BOT STATUS: running");
console.log("-------------------------");
// Sourcecode
// Discord.js Listeners
// Syntax: !send nickname > yourText || nickname must be listed on users.json
// Syntax: !listusers || lists all users registered to users.json
// Syntax: !help || Shows all functions
client.on('message', msg => {
let str = msg.content;
const subText = str.split(" "); // Splits up the messages into the word devided by spaces
if (subText[0] === '!send') { // Send Messages from Discord to Telegram
userNameID = methods.getUsers(subText[1], 1); // Read Users from Users.json (methods.getUsers() Method)
const finalMessage = str.split("-");
bot.telegram.sendMessage(userNameID, msg.author.username + ": " + finalMessage[1]); //Send Message to TelegramUser
logger.log("info", "[DISCORD>>TELEGRAM] Message from " + msg.author.username + " to " + subText[1]);
}
if (msg.content === '!listusers') { // Send Messages from Discord to Telegram
methods.listUsers(msg, 0);
logger.log("info", "Discord users listed (users.json) by " + msg.author.username);
}
if (msg.content === '!help') {
msg.channel.send("Commands: \n [!]send {nickname} > {yourText} -- Send text to user from Telegram \n [!]listusers -- Lists all Telegram users registered to the bot");
logger.log("info", "Help request by " + msg.author.username);
}
if (msg.content === '!auth') {
methods.addUsers(msg.author.username, msg.author.id, 2)
msg.channel.send(msg.author.username + " has been registered!");
logger.log("info", msg.author.username + " has been registered!");
}
});
bot.help((ctx) => {
ctx.reply("Commands: \n /auth -- Authenticate your Telegram ID \n /authgroup {nickname} \n /send {channelName} > {message} -- Message to discord channel \n /listusers -- List authenticated users");
logger.log("info", "Help request by " + ctx.from["username"]);
});
// User verifies himself and his ID will be added to users.json
// Syntax: /auth
bot.command('/auth', (ctx) => {
let userID = ctx.from["id"]; // get UserID
methods.addUsers(ctx.from["username"], userID, 1); // logged in addUsers()
ctx.reply("User " + ctx.from["username"] + " has been added!");
})
// Syntax: /authgroup {groupname}
bot.command('/authgroup', (ctx) => {
let userID = ctx.chat["id"]; // get UserID
let input = ctx.message["text"];
let subText = input.split(" ");
methods.addUsers(subText[1], userID, 1); // logged in addUsers()
ctx.reply("The group " + ctx.chat["title"] + " has been added as " + subText[1] + "!");
logger.log("info", "The group " + ctx.chat["title"] + " has been added as " + subText[1] + "!")
})
// Send messages from Telegram to Discord
// Syntax: /send yourChannelName yourText
bot.command('/send', (ctx) => {
let input = ctx.message["text"];
const subText = input.split(" ");
const channel = client.channels.find('name', subText[1]);
const finalMessage = input.split("-");
if (channel === null) {
client.users.get(methods.getUsers(subText[1], 2)).send(ctx.from["username"] + ": " +finalMessage[1]);
logger.log("info", "DM Message send to: " + subText[1]);
}
channel.send(ctx.from["username"] + ": " + finalMessage[1]);
logger.log("info", "[TELEGRAM>>DISCORD] Meesage from " + ctx.from["username"] + " to " + subText[1]);
})
bot.command('/listusers', (ctx) => {
methods.listUsers(ctx, 1);
logger.log("info", "Telegram users listed (users.json)");
})
bot.catch((err) => { // Catches Error
logger.log("error", err);
})