NadekoBot/NadekoBot/NadekoBot.cs

185 lines
7.8 KiB
C#
Raw Normal View History

2015-12-05 10:27:00 +00:00
using Discord;
using System;
using System.IO;
using Newtonsoft.Json;
using Parse;
using Discord.Commands;
2015-12-05 10:27:00 +00:00
using NadekoBot.Modules;
using Discord.Modules;
2015-12-31 19:40:09 +00:00
using Discord.Audio;
using NadekoBot.Extensions;
using System.Timers;
using System.Linq;
2016-01-26 20:42:22 +00:00
using System.Diagnostics;
2015-12-05 10:27:00 +00:00
2016-01-26 20:42:22 +00:00
namespace NadekoBot {
class NadekoBot {
2015-12-05 10:27:00 +00:00
public static DiscordClient client;
public static StatsCollector stats_collector;
2015-12-05 10:27:00 +00:00
public static string botMention;
public static string GoogleAPIKey = null;
public static ulong OwnerID;
2016-01-22 05:35:44 +00:00
public static User OwnerUser = null;
2015-12-30 04:44:36 +00:00
public static string password;
public static string TrelloAppKey;
2016-01-22 05:35:44 +00:00
public static bool ForwardMessages = false;
public static string BotVersion = "0.8-beta1";
2016-01-26 20:42:22 +00:00
public static int commandsRan = 0;
2015-12-05 10:27:00 +00:00
2016-01-26 20:42:22 +00:00
static void Main() {
2015-12-05 10:27:00 +00:00
//load credentials from credentials.json
Credentials c;
bool loadTrello = false;
2016-01-26 20:42:22 +00:00
try {
2015-12-05 10:27:00 +00:00
c = JsonConvert.DeserializeObject<Credentials>(File.ReadAllText("credentials.json"));
botMention = c.BotMention;
if (c.GoogleAPIKey == null || c.GoogleAPIKey == "") {
Console.WriteLine("No google api key found. You will not be able to use music and links won't be shortened.");
2016-01-11 20:37:48 +00:00
} else {
2016-01-21 22:22:55 +00:00
Console.WriteLine("Google API key provided.");
2016-01-11 20:37:48 +00:00
GoogleAPIKey = c.GoogleAPIKey;
}
if (c.TrelloAppKey == null || c.TrelloAppKey == "") {
Console.WriteLine("No trello appkey found. You will not be able to use trello commands.");
} else {
2016-01-21 22:22:55 +00:00
Console.WriteLine("Trello app key provided.");
TrelloAppKey = c.TrelloAppKey;
loadTrello = true;
}
2016-01-22 05:35:44 +00:00
if (c.ForwardMessages != true)
Console.WriteLine("Not forwarding messages.");
else {
ForwardMessages = true;
Console.WriteLine("Forwarding messages.");
}
2015-12-05 10:27:00 +00:00
OwnerID = c.OwnerID;
2015-12-30 04:44:36 +00:00
password = c.Password;
2016-01-26 20:42:22 +00:00
} catch (Exception ex) {
2015-12-05 10:27:00 +00:00
Console.WriteLine("Failed to load stuff from credentials.json, RTFM");
Console.ReadKey();
return;
}
//create new discord client
client = new DiscordClient();
2015-12-05 10:27:00 +00:00
//create a command service
2016-01-26 20:42:22 +00:00
var commandService = new CommandService(new CommandServiceConfig {
2015-12-05 10:27:00 +00:00
CommandChar = null,
HelpMode = HelpMode.Disable
});
//init parse
if (c.ParseKey != null && c.ParseID != null && c.ParseID != "" && c.ParseKey != "") {
ParseClient.Initialize(c.ParseID, c.ParseKey);
//monitor commands for logging
stats_collector = new StatsCollector(commandService);
} else {
2016-01-21 22:22:55 +00:00
Console.WriteLine("Parse key and/or ID not found. Logging disabled.");
}
2016-01-22 05:35:44 +00:00
//reply to personal messages and forward if enabled.
client.MessageReceived += Client_MessageReceived;
2015-12-05 10:27:00 +00:00
//add command service
2015-12-30 04:44:36 +00:00
var commands = client.Services.Add<CommandService>(commandService);
2016-01-26 20:42:22 +00:00
2016-01-26 20:43:50 +00:00
//count commands ran
client.Commands().CommandExecuted += (s, e) => commandsRan++;
2015-12-05 10:27:00 +00:00
//create module service
2015-12-30 04:44:36 +00:00
var modules = client.Services.Add<ModuleService>(new ModuleService());
2015-12-05 10:27:00 +00:00
2015-12-31 19:40:09 +00:00
//add audio service
var audio = client.Services.Add<AudioService>(new AudioService(new AudioServiceConfig() {
Channels = 2,
2016-01-22 05:35:44 +00:00
EnableEncryption = false,
EnableMultiserver = true,
Bitrate = 128,
2015-12-31 19:40:09 +00:00
}));
2015-12-05 10:27:00 +00:00
//install modules
2016-01-19 05:06:20 +00:00
modules.Add(new Administration(), "Administration", ModuleFilter.None);
modules.Add(new Conversations(), "Conversations", ModuleFilter.None);
modules.Add(new Gambling(), "Gambling", ModuleFilter.None);
modules.Add(new Games(), "Games", ModuleFilter.None);
modules.Add(new Music(), "Music", ModuleFilter.None);
modules.Add(new Searches(), "Searches", ModuleFilter.None);
2016-01-26 20:42:22 +00:00
if (loadTrello)
2016-01-19 05:06:20 +00:00
modules.Add(new Trello(), "Trello", ModuleFilter.None);
2015-12-05 10:27:00 +00:00
//run the bot
2016-01-26 20:42:22 +00:00
client.ExecuteAndWait(async () => {
await client.Connect(c.Username, c.Password);
2016-01-21 22:22:55 +00:00
Console.WriteLine("-----------------");
Console.WriteLine(GetStats());
Console.WriteLine("-----------------");
2016-01-22 05:35:44 +00:00
foreach (var serv in client.Servers) {
if ((OwnerUser = serv.GetUser(OwnerID)) != null)
return;
}
2015-12-05 10:27:00 +00:00
});
Console.WriteLine("Exiting...");
Console.ReadKey();
}
public static string GetStats() =>
2016-01-26 20:42:22 +00:00
"Author: Kwoth" +
$"\nDiscord.Net version: {DiscordConfig.LibVersion}"+
$"\nRuntime: {client.GetRuntime()}" +
$"\nBot Version: {BotVersion}"+
$"\nLogged in as: {client.CurrentUser.Name}" +
$"\nBot id: {client.CurrentUser.Id}" +
$"\nUptime: {GetUptimeString()}" +
$"\nServers: {client.Servers.Count()}" +
$"\nChannels: {client.Servers.Sum(s => s.AllChannels.Count())}" +
2016-01-26 20:51:54 +00:00
$"\nUsers: {client.Servers.SelectMany(x => x.Users.Select(y => y.Id)).Count()} ({client.Servers.SelectMany(x => x.Users.Select(y => y.Id)).Distinct().Count()} unique) ({client.Servers.SelectMany(x => x.Users.Where(y => y.Status != UserStatus.Offline).Select(y => y.Id)).Distinct().Count()} online)" +
$"\nHeap: {Math.Round(GC.GetTotalMemory(true) / (1024.0 * 1024.0), 2).ToString()}MB" +
$"\nCommands Ran this session: {commandsRan}";
2016-01-26 20:42:22 +00:00
public static string GetUptimeString() {
var time = (DateTime.Now - Process.GetCurrentProcess().StartTime);
return time.Days + " days, " + time.Hours + " hours, and " + time.Minutes + " minutes.";
2016-01-26 20:42:22 +00:00
}
static bool repliedRecently = false;
private static async void Client_MessageReceived(object sender, MessageEventArgs e) {
if (e.Server != null || e.User.Id == client.CurrentUser.Id) return;
2016-01-23 17:05:10 +00:00
//just ban this trash AutoModerator
if (e.User.Id == 105309315895693312)
return; // FU
try {
2016-01-23 16:21:50 +00:00
await (await client.GetInvite(e.Message.Text)).Accept();
await e.Send("I got in!");
return;
} catch (Exception) {
if (e.User.Id == 109338686889476096) { //carbonitex invite
await e.Send("Failed to join the server.");
return;
}
}
if (ForwardMessages && OwnerUser != null)
await OwnerUser.SendMessage(e.User +": ```\n"+e.Message.Text+"\n```");
2016-01-23 16:21:50 +00:00
if (repliedRecently = !repliedRecently) {
2016-01-22 19:20:59 +00:00
await e.Send("You can type `-h` or `-help` or `@MyName help` in any of the channels I am in and I will send you a message with my commands.\n Or you can find out what i do here: https://github.com/Kwoth/NadekoBot\nYou can also just send me an invite link to a server and I will join it.\nIf you don't want me on your server, you can simply ban me ;(\nBot Creator's server: https://discord.gg/0ehQwTK2RBhxEi0X");
Timer t = new Timer();
t.Interval = 2000;
t.Start();
t.Elapsed += (s, ev) => {
repliedRecently = !repliedRecently;
t.Stop();
t.Dispose();
};
}
}
2015-12-05 10:27:00 +00:00
}
}