2015-12-05 10:27:00 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Newtonsoft.Json;
|
2015-12-09 20:47:02 +00:00
|
|
|
|
using Discord.Commands;
|
2015-12-05 10:27:00 +00:00
|
|
|
|
using NadekoBot.Modules;
|
2015-12-09 20:47:02 +00:00
|
|
|
|
using Discord.Modules;
|
2015-12-31 19:40:09 +00:00
|
|
|
|
using Discord.Audio;
|
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;
|
|
|
|
|
using NadekoBot.Classes.JSONModels;
|
2016-03-02 05:00:44 +00:00
|
|
|
|
using NadekoBot.Commands;
|
2015-12-05 10:27:00 +00:00
|
|
|
|
|
2016-01-26 20:42:22 +00:00
|
|
|
|
namespace NadekoBot {
|
2016-03-04 17:58:19 +00:00
|
|
|
|
internal class NadekoBot {
|
2016-02-29 09:59:40 +00:00
|
|
|
|
public static DiscordClient Client;
|
|
|
|
|
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; } = "";
|
|
|
|
|
|
|
|
|
|
private static Channel OwnerPrivateChannel { get; set; }
|
2015-12-05 10:27:00 +00:00
|
|
|
|
|
2016-03-01 11:44:30 +00:00
|
|
|
|
private static void Main() {
|
2016-03-03 06:43:50 +00:00
|
|
|
|
Console.OutputEncoding = Encoding.Unicode;
|
2016-03-03 20:24:07 +00:00
|
|
|
|
// generate credentials example so people can know about the changes i make
|
|
|
|
|
try {
|
|
|
|
|
File.WriteAllText("credentials_example.json", JsonConvert.SerializeObject(new Credentials(), Formatting.Indented));
|
|
|
|
|
File.WriteAllText("data/config_example.json", JsonConvert.SerializeObject(new Configuration(), Formatting.Indented));
|
2016-03-04 17:58:19 +00:00
|
|
|
|
} catch {
|
2016-03-03 20:24:07 +00:00
|
|
|
|
Console.WriteLine("Failed writing credentials_example.json or data/config_example.json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Config = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText("data/config.json"));
|
2016-03-04 17:58:19 +00:00
|
|
|
|
} catch {
|
2016-03-03 20:24:07 +00:00
|
|
|
|
Console.WriteLine("Failed loading configuration.");
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 20:42: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-02-29 17:28:16 +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
|
|
|
|
|
if (string.IsNullOrWhiteSpace(Creds.Password)) {
|
|
|
|
|
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-02-29 09:59:40 +00:00
|
|
|
|
Client = new DiscordClient(new DiscordConfigBuilder() {
|
2016-03-06 07:50:25 +00:00
|
|
|
|
MessageCacheSize = 10,
|
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-02-13 23:57:52 +00:00
|
|
|
|
var commandService = new CommandService(new CommandServiceConfigBuilder {
|
|
|
|
|
AllowMentionPrefix = false,
|
|
|
|
|
CustomPrefixHandler = m => 0,
|
2016-02-19 03:53:54 +00:00
|
|
|
|
HelpMode = HelpMode.Disabled,
|
2016-02-25 01:44:39 +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-02-25 21:29:58 +00:00
|
|
|
|
try {
|
|
|
|
|
await e.Channel.SendMessage(e.Exception.Message);
|
2016-02-29 17:28:16 +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-02-29 17:28:16 +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-01-19 05:06:20 +00:00
|
|
|
|
modules.Add(new Administration(), "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);
|
|
|
|
|
modules.Add(new Gambling(), "Gambling", ModuleFilter.None);
|
|
|
|
|
modules.Add(new Games(), "Games", ModuleFilter.None);
|
2016-03-09 12:48:46 +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-02-29 17:28:16 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey))
|
|
|
|
|
modules.Add(new Trello(), "Trello", ModuleFilter.None);
|
2015-12-05 10:27:00 +00:00
|
|
|
|
|
|
|
|
|
//run the bot
|
2016-02-29 09:59:40 +00:00
|
|
|
|
Client.ExecuteAndWait(async () => {
|
2016-02-16 21:51:39 +00:00
|
|
|
|
try {
|
2016-02-29 09:59:40 +00:00
|
|
|
|
await Client.Connect(Creds.Username, Creds.Password);
|
2016-02-29 17:28:16 +00:00
|
|
|
|
} catch (Exception ex) {
|
2016-02-16 21:51:39 +00:00
|
|
|
|
Console.WriteLine($"Probably wrong EMAIL or PASSWORD.\n{ex.Message}");
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
Console.WriteLine(ex);
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
return;
|
|
|
|
|
}
|
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-02-11 15:09:24 +00:00
|
|
|
|
try {
|
2016-02-29 17:28:16 +00:00
|
|
|
|
OwnerPrivateChannel = await Client.CreatePrivateChannel(Creds.OwnerIds[0]);
|
|
|
|
|
} catch {
|
|
|
|
|
Console.WriteLine("Failed creating private channel with the first owner listed in credentials.json");
|
2016-01-22 05:35:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-11 15:09:24 +00:00
|
|
|
|
Classes.Permissions.PermissionsHandler.Initialize();
|
2016-02-28 03:30:16 +00:00
|
|
|
|
|
2016-02-29 17:28:16 +00:00
|
|
|
|
Client.ClientAPI.SendingRequest += (s, e) => {
|
|
|
|
|
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
|
|
|
|
};
|
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);
|
|
|
|
|
|
|
|
|
|
public static bool IsOwner(User u) => IsOwner(u.Id);
|
|
|
|
|
|
|
|
|
|
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-01-14 13:33:16 +00:00
|
|
|
|
private static async void Client_MessageReceived(object sender, MessageEventArgs e) {
|
2016-02-25 21:29:58 +00:00
|
|
|
|
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-03-03 20:24:07 +00:00
|
|
|
|
if (!NadekoBot.Config.DontJoinServers) {
|
2016-02-25 21:29:58 +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-02-29 17:28:16 +00:00
|
|
|
|
} catch {
|
2016-02-25 21:29:58 +00:00
|
|
|
|
if (e.User.Id == 109338686889476096) { //carbonitex invite
|
|
|
|
|
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-03-10 14:05:38 +00:00
|
|
|
|
if (Config.ForwardMessages && 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;
|
|
|
|
|
await e.Channel.SendMessage(HelpCommand.HelpString);
|
|
|
|
|
await Task.Run(async () => {
|
|
|
|
|
await Task.Delay(2000);
|
2016-02-25 21:29:58 +00:00
|
|
|
|
repliedRecently = true;
|
2016-02-29 17:28:16 +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
|