2015-12-05 10:27:00 +00:00
|
|
|
|
using Discord;
|
2016-03-25 11:48:22 +00:00
|
|
|
|
using Discord.Audio;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Discord.Modules;
|
|
|
|
|
using NadekoBot.Classes.JSONModels;
|
|
|
|
|
using NadekoBot.Commands;
|
|
|
|
|
using NadekoBot.Modules;
|
2016-03-29 13:20:18 +00:00
|
|
|
|
using NadekoBot.Modules.Administration;
|
2016-03-25 11:48:22 +00:00
|
|
|
|
using NadekoBot.Modules.Gambling;
|
2016-04-01 21:07:49 +00:00
|
|
|
|
using NadekoBot.Modules.Games;
|
2016-03-27 12:55:49 +00:00
|
|
|
|
using NadekoBot.Modules.Pokemon;
|
2016-03-28 14:44:39 +00:00
|
|
|
|
using NadekoBot.Modules.Translator;
|
2016-03-25 11:48:22 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2015-12-05 10:27:00 +00:00
|
|
|
|
using System;
|
2016-03-15 19:21:27 +00:00
|
|
|
|
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;
|
2016-03-03 06:43:50 +00:00
|
|
|
|
using System.Text;
|
2016-02-29 17:28:16 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2015-12-05 10:27:00 +00:00
|
|
|
|
|
2016-03-25 11:48:22 +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; }
|
2016-03-03 20:24:07 +00:00
|
|
|
|
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; } = "";
|
2016-04-10 00:38:46 +00:00
|
|
|
|
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
|
|
|
|
|
2016-03-25 11:48:22 +00:00
|
|
|
|
private static void Main()
|
|
|
|
|
{
|
2016-03-03 06:43:50 +00:00
|
|
|
|
Console.OutputEncoding = Encoding.Unicode;
|
2016-03-15 15:32:03 +00:00
|
|
|
|
|
2016-03-15 19:21:27 +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();
|
2016-03-15 19:21:27 +00:00
|
|
|
|
// 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();
|
2016-03-03 20:24:07 +00:00
|
|
|
|
// generate credentials example so people can know about the changes i make
|
2016-03-25 11:48:22 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-03-03 20:24:07 +00:00
|
|
|
|
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));
|
2016-03-25 11:48:22 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2016-03-03 20:24:07 +00:00
|
|
|
|
Console.WriteLine("Failed writing credentials_example.json or data/config_example.json");
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 11:48:22 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-03-03 20:24:07 +00:00
|
|
|
|
Config = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText("data/config.json"));
|
2016-03-15 19:21:27 +00:00
|
|
|
|
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-03-25 11:48:22 +00:00
|
|
|
|
}
|
2016-04-09 12:15:02 +00:00
|
|
|
|
catch (Exception ex)
|
2016-03-25 11:48:22 +00:00
|
|
|
|
{
|
2016-03-03 20:24:07 +00:00
|
|
|
|
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;
|
2016-03-03 20:24:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 11:48:22 +00:00
|
|
|
|
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"));
|
2016-03-25 11:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 13:16:31 +00:00
|
|
|
|
//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))
|
2016-03-25 11:48:22 +00:00
|
|
|
|
{
|
2016-03-07 13:16:31 +00:00
|
|
|
|
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.");
|
2016-03-03 20:24:07 +00:00
|
|
|
|
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
|
2016-03-25 11:48:22 +00:00
|
|
|
|
Client = new DiscordClient(new DiscordConfigBuilder()
|
|
|
|
|
{
|
2016-03-06 07:50:25 +00:00
|
|
|
|
MessageCacheSize = 10,
|
2016-04-01 06:17:06 +00:00
|
|
|
|
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 ?? "-"}"),
|
2016-02-10 14:43:44 +00:00
|
|
|
|
});
|
2015-12-05 10:27:00 +00:00
|
|
|
|
|
|
|
|
|
//create a command service
|
2016-03-25 11:48:22 +00:00
|
|
|
|
var commandService = new CommandService(new CommandServiceConfigBuilder
|
|
|
|
|
{
|
2016-02-13 23:57:52 +00:00
|
|
|
|
AllowMentionPrefix = false,
|
|
|
|
|
CustomPrefixHandler = m => 0,
|
2016-02-19 03:53:54 +00:00
|
|
|
|
HelpMode = HelpMode.Disabled,
|
2016-03-25 11:48:22 +00:00
|
|
|
|
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;
|
2016-03-25 11:48:22 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-02-25 21:29:58 +00:00
|
|
|
|
await e.Channel.SendMessage(e.Exception.Message);
|
2016-03-25 11:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
catch { }
|
2016-02-25 01:44:39 +00:00
|
|
|
|
}
|
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;
|
2016-01-29 12:28:51 +00:00
|
|
|
|
|
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
|
2016-03-25 11:48:22 +00:00
|
|
|
|
Client.AddService<AudioService>(new AudioService(new AudioServiceConfigBuilder()
|
|
|
|
|
{
|
2016-01-05 04:52:11 +00:00
|
|
|
|
Channels = 2,
|
2016-01-22 05:35:44 +00:00
|
|
|
|
EnableEncryption = false,
|
2016-01-28 03:38:26 +00:00
|
|
|
|
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);
|
2016-02-15 22:06:14 +00:00
|
|
|
|
modules.Add(new Help(), "Help", ModuleFilter.None);
|
2016-02-11 03:30:19 +00:00
|
|
|
|
modules.Add(new PermissionModule(), "Permissions", ModuleFilter.None);
|
2016-01-19 05:06:20 +00:00
|
|
|
|
modules.Add(new Conversations(), "Conversations", ModuleFilter.None);
|
2016-03-25 11:48:22 +00:00
|
|
|
|
modules.Add(new GamblingModule(), "Gambling", ModuleFilter.None);
|
2016-04-01 21:07:49 +00:00
|
|
|
|
modules.Add(new GamesModule(), "Games", ModuleFilter.None);
|
2016-04-10 00:55:14 +00:00
|
|
|
|
modules.Add(new Music(), "Music", ModuleFilter.None);
|
2016-01-19 05:06:20 +00:00
|
|
|
|
modules.Add(new Searches(), "Searches", ModuleFilter.None);
|
2016-02-10 13:39:11 +00:00
|
|
|
|
modules.Add(new NSFW(), "NSFW", ModuleFilter.None);
|
2016-03-11 19:13:48 +00:00
|
|
|
|
modules.Add(new ClashOfClans(), "ClashOfClans", ModuleFilter.None);
|
2016-03-28 14:44:39 +00:00
|
|
|
|
modules.Add(new PokemonModule(), "Pokegame", ModuleFilter.None);
|
2016-03-28 13:36:15 +00:00
|
|
|
|
modules.Add(new TranslatorModule(), "Translator", ModuleFilter.None);
|
2016-03-12 10:56:17 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey))
|
2016-02-29 17:28:16 +00:00
|
|
|
|
modules.Add(new Trello(), "Trello", ModuleFilter.None);
|
2015-12-05 10:27:00 +00:00
|
|
|
|
|
|
|
|
|
//run the bot
|
2016-03-25 11:48:22 +00:00
|
|
|
|
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-03-25 11:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
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.");
|
2016-02-16 21:51:39 +00:00
|
|
|
|
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);
|
2016-01-22 07:17:16 +00:00
|
|
|
|
Console.WriteLine("-----------------");
|
2016-02-28 17:42:34 +00:00
|
|
|
|
Console.WriteLine(await NadekoStats.Instance.GetStats());
|
2016-01-22 07:17:16 +00:00
|
|
|
|
Console.WriteLine("-----------------");
|
2016-01-22 05:35:44 +00:00
|
|
|
|
|
2016-03-25 11:48:22 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-02-29 17:28:16 +00:00
|
|
|
|
OwnerPrivateChannel = await Client.CreatePrivateChannel(Creds.OwnerIds[0]);
|
2016-03-25 11:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 11:48:22 +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-10 00:38:46 +00:00
|
|
|
|
Classes.Permissions.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-01-22 07:17:16 +00:00
|
|
|
|
|
2016-02-29 17:28:16 +00:00
|
|
|
|
public static bool IsOwner(ulong id) => Creds.OwnerIds.Contains(id);
|
|
|
|
|
|
2016-03-25 11:48:22 +00:00
|
|
|
|
public async Task SendMessageToOwner(string message)
|
|
|
|
|
{
|
2016-03-10 14:05:38 +00:00
|
|
|
|
if (Config.ForwardMessages && OwnerPrivateChannel != null)
|
2016-02-29 17:28:16 +00:00
|
|
|
|
await OwnerPrivateChannel.SendMessage(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool repliedRecently = false;
|
2016-03-25 11:48:22 +00:00
|
|
|
|
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;
|
2016-02-25 21:29:58 +00:00
|
|
|
|
if (PollCommand.ActivePolls.SelectMany(kvp => kvp.Key.Users.Select(u => u.Id)).Contains(e.User.Id)) return;
|
2016-03-10 14:05:38 +00:00
|
|
|
|
if (ConfigHandler.IsBlackListed(e))
|
2016-03-03 20:24:07 +00:00
|
|
|
|
return;
|
2016-03-04 17:58:19 +00:00
|
|
|
|
|
2016-04-14 20:09:19 +00:00
|
|
|
|
if (!NadekoBot.Config.DontJoinServers && !IsBot)
|
2016-03-25 11:48:22 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-02-29 09:59:40 +00:00
|
|
|
|
await (await Client.GetInvite(e.Message.Text)).Accept();
|
2016-02-25 21:29:58 +00:00
|
|
|
|
await e.Channel.SendMessage("I got in!");
|
2016-02-19 05:23:02 +00:00
|
|
|
|
return;
|
2016-03-25 11:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if (e.User.Id == 109338686889476096)
|
|
|
|
|
{ //carbonitex invite
|
2016-02-25 21:29:58 +00:00
|
|
|
|
await e.Channel.SendMessage("Failed to join the server.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-19 05:23:02 +00:00
|
|
|
|
}
|
2016-01-15 19:54:57 +00:00
|
|
|
|
|
2016-04-10 00:38:46 +00:00
|
|
|
|
if (Config.ForwardMessages && !NadekoBot.Creds.OwnerIds.Contains(e.User.Id) && OwnerPrivateChannel != null)
|
2016-02-25 21:29:58 +00:00
|
|
|
|
await OwnerPrivateChannel.SendMessage(e.User + ": ```\n" + e.Message.Text + "\n```");
|
|
|
|
|
|
2016-02-29 17:28:16 +00:00
|
|
|
|
if (repliedRecently) return;
|
|
|
|
|
|
|
|
|
|
repliedRecently = true;
|
2016-04-10 00:38:46 +00:00
|
|
|
|
if (e.Message.RawText != "-h")
|
|
|
|
|
await e.Channel.SendMessage(HelpCommand.DMHelpString);
|
2016-03-14 01:10:12 +00:00
|
|
|
|
await Task.Delay(2000);
|
|
|
|
|
repliedRecently = false;
|
2016-03-25 11:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
catch { }
|
2016-01-14 13:33:16 +00:00
|
|
|
|
}
|
2015-12-05 10:27:00 +00:00
|
|
|
|
}
|
2016-02-07 20:51:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//95520984584429568 meany
|