NadekoBot/NadekoBot/NadekoBot.cs

296 lines
12 KiB
C#
Raw Normal View History

2015-12-05 10:27:00 +00:00
using Discord;
using Discord.Audio;
using Discord.Commands;
using Discord.Modules;
using NadekoBot.Classes.JSONModels;
2016-04-14 22:17:29 +00:00
using NadekoBot.Classes.Help.Commands;
2016-03-29 13:20:18 +00:00
using NadekoBot.Modules.Administration;
using NadekoBot.Modules.ClashOfClans;
using NadekoBot.Modules.Conversations;
using NadekoBot.Modules.Gambling;
using NadekoBot.Modules.Games;
2016-04-14 21:30:13 +00:00
using NadekoBot.Modules.Games.Commands;
using NadekoBot.Modules.Help;
using NadekoBot.Modules.Music;
using NadekoBot.Modules.NSFW;
using NadekoBot.Modules.Permissions;
2016-04-14 22:17:29 +00:00
using NadekoBot.Modules.Permissions.Classes;
2016-03-27 12:55:49 +00:00
using NadekoBot.Modules.Pokemon;
2016-04-14 21:30:13 +00:00
using NadekoBot.Modules.Searches;
2016-03-28 14:44:39 +00:00
using NadekoBot.Modules.Translator;
using NadekoBot.Modules.Trello;
using Newtonsoft.Json;
2015-12-05 10:27:00 +00:00
using System;
using System.Collections.Generic;
2015-12-05 10:27:00 +00:00
using System.IO;
2016-02-06 13:43:03 +00:00
using System.Linq;
using System.Text;
2016-02-29 17:28:16 +00:00
using System.Threading.Tasks;
2015-12-05 10:27:00 +00:00
namespace NadekoBot
{
public class NadekoBot
{
2016-03-31 05:50:34 +00:00
public static DiscordClient Client { get; private set; }
2016-02-29 09:59:40 +00:00
public static Credentials Creds { get; set; }
public static Configuration Config { get; set; }
2016-03-04 17:58:19 +00:00
public static LocalizedStrings Locale { get; set; } = new LocalizedStrings();
2016-02-29 17:28:16 +00:00
public static string BotMention { get; set; } = "";
public static bool Ready { get; set; } = false;
2016-04-14 20:09:19 +00:00
public static bool IsBot { get; set; } = false;
2016-02-29 17:28:16 +00:00
private static Channel OwnerPrivateChannel { get; set; }
2015-12-05 10:27:00 +00:00
private static void Main()
{
Console.OutputEncoding = Encoding.Unicode;
2016-03-15 15:32:03 +00:00
//var lines = File.ReadAllLines("data/input.txt");
//HashSet<dynamic> list = new HashSet<dynamic>();
//for (int i = 0; i < lines.Length; i += 3) {
2016-03-22 05:12:56 +00:00
// dynamic obj = new JArray();
// obj.Text = lines[i];
// obj.Author = lines[i + 1];
// if (obj.Author.StartsWith("-"))
// obj.Author = obj.Author.Substring(1, obj.Author.Length - 1).Trim();
// list.Add(obj);
//}
//File.WriteAllText("data/quotes.json", Newtonsoft.Json.JsonConvert.SerializeObject(list, Formatting.Indented));
//Console.ReadKey();
// generate credentials example so people can know about the changes i make
try
{
File.WriteAllText("data/config_example.json", JsonConvert.SerializeObject(new Configuration(), Formatting.Indented));
2016-03-22 05:12:56 +00:00
if (!File.Exists("data/config.json"))
File.Copy("data/config_example.json", "data/config.json");
File.WriteAllText("credentials_example.json", JsonConvert.SerializeObject(new Credentials(), Formatting.Indented));
}
catch
{
Console.WriteLine("Failed writing credentials_example.json or data/config_example.json");
}
try
{
Config = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText("data/config.json"));
Config.Quotes = JsonConvert.DeserializeObject<List<Quote>>(File.ReadAllText("data/quotes.json"));
2016-04-03 10:52:59 +00:00
Config.PokemonTypes = JsonConvert.DeserializeObject<List<PokemonType>>(File.ReadAllText("data/PokemonTypes.json"));
}
2016-04-09 12:15:02 +00:00
catch (Exception ex)
{
Console.WriteLine("Failed loading configuration.");
2016-04-09 12:15:02 +00:00
Console.WriteLine(ex);
2016-03-22 05:12:56 +00:00
Console.ReadKey();
return;
}
try
{
2016-02-29 17:28:16 +00:00
//load credentials from credentials.json
2016-02-29 09:59:40 +00:00
Creds = JsonConvert.DeserializeObject<Credentials>(File.ReadAllText("credentials.json"));
}
catch (Exception ex)
{
2016-01-30 11:08:30 +00:00
Console.WriteLine($"Failed to load stuff from credentials.json, RTFM\n{ex.Message}");
2015-12-05 10:27:00 +00:00
Console.ReadKey();
return;
}
//if password is not entered, prompt for password
2016-04-14 20:09:19 +00:00
if (string.IsNullOrWhiteSpace(Creds.Password) && string.IsNullOrWhiteSpace(Creds.Token))
{
Console.WriteLine("Password blank. Please enter your password:\n");
Creds.Password = Console.ReadLine();
}
2016-02-29 17:28:16 +00:00
Console.WriteLine(string.IsNullOrWhiteSpace(Creds.GoogleAPIKey)
? "No google api key found. You will not be able to use music and links won't be shortened."
: "Google API key provided.");
Console.WriteLine(string.IsNullOrWhiteSpace(Creds.TrelloAppKey)
? "No trello appkey found. You will not be able to use trello commands."
: "Trello app key provided.");
Console.WriteLine(Config.ForwardMessages != true
2016-02-29 17:28:16 +00:00
? "Not forwarding messages."
: "Forwarding private messages to owner.");
Console.WriteLine(string.IsNullOrWhiteSpace(Creds.SoundCloudClientID)
? "No soundcloud Client ID found. Soundcloud streaming is disabled."
: "SoundCloud streaming enabled.");
BotMention = $"<@{Creds.BotId}>";
//create new discord client and log
Client = new DiscordClient(new DiscordConfigBuilder()
{
2016-03-06 07:50:25 +00:00
MessageCacheSize = 10,
ConnectionTimeout = 120000,
2016-02-28 03:30:16 +00:00
LogLevel = LogSeverity.Warning,
2016-02-29 17:28:16 +00:00
LogHandler = (s, e) =>
Console.WriteLine($"Severity: {e.Severity}" +
$"Message: {e.Message}" +
$"ExceptionMessage: {e.Exception?.Message ?? "-"}"),
});
2015-12-05 10:27:00 +00:00
//create a command service
var commandService = new CommandService(new CommandServiceConfigBuilder
{
AllowMentionPrefix = false,
CustomPrefixHandler = m => 0,
2016-02-19 03:53:54 +00:00
HelpMode = HelpMode.Disabled,
ErrorHandler = async (s, e) =>
{
2016-02-29 17:28:16 +00:00
if (e.ErrorType != CommandErrorType.BadPermissions)
return;
if (string.IsNullOrWhiteSpace(e.Exception?.Message))
return;
try
{
await e.Channel.SendMessage(e.Exception.Message);
}
catch { }
}
2015-12-05 10:27:00 +00:00
});
2016-02-19 03:53:54 +00:00
2016-01-22 05:35:44 +00:00
//reply to personal messages and forward if enabled.
2016-02-29 09:59:40 +00:00
Client.MessageReceived += Client_MessageReceived;
2015-12-05 10:27:00 +00:00
//add command service
2016-02-29 17:28:16 +00:00
Client.AddService<CommandService>(commandService);
2016-01-26 20:42:22 +00:00
2015-12-05 10:27:00 +00:00
//create module service
2016-02-29 09:59:40 +00:00
var modules = Client.AddService<ModuleService>(new ModuleService());
2015-12-05 10:27:00 +00:00
2015-12-31 19:40:09 +00:00
//add audio service
Client.AddService<AudioService>(new AudioService(new AudioServiceConfigBuilder()
{
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-03-29 13:20:18 +00:00
modules.Add(new AdministrationModule(), "Administration", ModuleFilter.None);
modules.Add(new HelpModule(), "Help", ModuleFilter.None);
modules.Add(new PermissionModule(), "Permissions", ModuleFilter.None);
2016-01-19 05:06:20 +00:00
modules.Add(new Conversations(), "Conversations", ModuleFilter.None);
modules.Add(new GamblingModule(), "Gambling", ModuleFilter.None);
modules.Add(new GamesModule(), "Games", ModuleFilter.None);
modules.Add(new MusicModule(), "Music", ModuleFilter.None);
modules.Add(new SearchesModule(), "Searches", ModuleFilter.None);
modules.Add(new NSFWModule(), "NSFW", ModuleFilter.None);
modules.Add(new ClashOfClansModule(), "ClashOfClans", ModuleFilter.None);
2016-03-28 14:44:39 +00:00
modules.Add(new PokemonModule(), "Pokegame", ModuleFilter.None);
modules.Add(new TranslatorModule(), "Translator", ModuleFilter.None);
2016-03-12 10:56:17 +00:00
if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey))
modules.Add(new TrelloModule(), "Trello", ModuleFilter.None);
2015-12-05 10:27:00 +00:00
//run the bot
Client.ExecuteAndWait(async () =>
{
try
{
2016-04-09 12:15:02 +00:00
if (string.IsNullOrWhiteSpace(Creds.Token))
await Client.Connect(Creds.Username, Creds.Password);
else
2016-04-14 20:09:19 +00:00
{
2016-04-09 12:15:02 +00:00
await Client.Connect(Creds.Token);
2016-04-14 20:09:19 +00:00
IsBot = true;
}
2016-04-14 20:32:39 +00:00
Console.WriteLine(NadekoBot.Client.CurrentUser.Id);
}
catch (Exception ex)
{
2016-04-09 12:15:02 +00:00
if (string.IsNullOrWhiteSpace(Creds.Token))
Console.WriteLine($"Probably wrong EMAIL or PASSWORD.");
else
Console.WriteLine($"Token is wrong. Don't set a token if you don't have an official BOT account.");
Console.WriteLine(ex);
Console.ReadKey();
return;
}
2016-04-14 20:09:19 +00:00
2016-04-14 20:09:35 +00:00
//await Task.Delay(90000);
Console.WriteLine("-----------------");
Console.WriteLine(await NadekoStats.Instance.GetStats());
Console.WriteLine("-----------------");
2016-01-22 05:35:44 +00:00
try
{
2016-02-29 17:28:16 +00:00
OwnerPrivateChannel = await Client.CreatePrivateChannel(Creds.OwnerIds[0]);
}
catch
{
2016-02-29 17:28:16 +00:00
Console.WriteLine("Failed creating private channel with the first owner listed in credentials.json");
2016-01-22 05:35:44 +00:00
}
Client.ClientAPI.SendingRequest += (s, e) =>
{
2016-02-29 17:28:16 +00:00
var request = e.Request as Discord.API.Client.Rest.SendMessageRequest;
if (request == null) return;
request.Content = request.Content?.Replace("@everyone", "@everyοne") ?? "_error_";
if (string.IsNullOrWhiteSpace(request.Content))
e.Cancel = true;
2016-02-02 12:18:13 +00:00
};
2016-04-14 22:17:29 +00:00
PermissionsHandler.Initialize();
NadekoBot.Ready = true;
2015-12-05 10:27:00 +00:00
});
Console.WriteLine("Exiting...");
Console.ReadKey();
2016-02-19 03:53:54 +00:00
}
2016-02-29 17:28:16 +00:00
public static bool IsOwner(ulong id) => Creds.OwnerIds.Contains(id);
public async Task SendMessageToOwner(string message)
{
if (Config.ForwardMessages && OwnerPrivateChannel != null)
2016-02-29 17:28:16 +00:00
await OwnerPrivateChannel.SendMessage(message);
}
private static bool repliedRecently = false;
private static async void Client_MessageReceived(object sender, MessageEventArgs e)
{
try
{
2016-02-29 09:59:40 +00:00
if (e.Server != null || e.User.Id == Client.CurrentUser.Id) return;
if (PollCommand.ActivePolls.SelectMany(kvp => kvp.Key.Users.Select(u => u.Id)).Contains(e.User.Id)) return;
if (ConfigHandler.IsBlackListed(e))
return;
2016-03-04 17:58:19 +00:00
2016-04-14 20:09:19 +00:00
if (!NadekoBot.Config.DontJoinServers && !IsBot)
{
try
{
2016-02-29 09:59:40 +00:00
await (await Client.GetInvite(e.Message.Text)).Accept();
await e.Channel.SendMessage("I got in!");
return;
}
catch
{
if (e.User.Id == 109338686889476096)
{ //carbonitex invite
await e.Channel.SendMessage("Failed to join the server.");
return;
}
}
}
if (Config.ForwardMessages && !NadekoBot.Creds.OwnerIds.Contains(e.User.Id) && OwnerPrivateChannel != null)
await OwnerPrivateChannel.SendMessage(e.User + ": ```\n" + e.Message.Text + "\n```");
2016-02-29 17:28:16 +00:00
if (repliedRecently) return;
repliedRecently = true;
if (e.Message.RawText != "-h")
await e.Channel.SendMessage(HelpCommand.DMHelpString);
await Task.Delay(2000);
repliedRecently = false;
}
catch { }
}
2015-12-05 10:27:00 +00:00
}
2016-02-07 20:51:05 +00:00
}
//95520984584429568 meany