NadekoBot/NadekoBot/NadekoBot.cs

208 lines
9.0 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 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;
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;
using NadekoBot.Classes.JSONModels;
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; }
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() {
Console.OutputEncoding = Encoding.Unicode;
// 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 {
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 {
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;
}
//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.");
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 ?? "-"}"),
});
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);
2016-02-29 17:28:16 +00:00
} 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
2016-02-29 17:28:16 +00:00
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-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);
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);
modules.Add(new Music(), "Music", ModuleFilter.None);
2016-01-19 05:06:20 +00:00
modules.Add(new Searches(), "Searches", ModuleFilter.None);
modules.Add(new NSFW(), "NSFW", 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 () => {
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) {
Console.WriteLine($"Probably wrong EMAIL or PASSWORD.\n{ex.Message}");
Console.ReadKey();
Console.WriteLine(ex);
Console.ReadKey();
return;
}
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 {
Console.WriteLine("Failed creating private channel with the first owner listed in credentials.json");
2016-01-22 05:35:44 +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-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) {
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
if (!NadekoBot.Config.DontJoinServers) {
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;
2016-02-29 17:28:16 +00:00
} catch {
if (e.User.Id == 109338686889476096) { //carbonitex invite
await e.Channel.SendMessage("Failed to join the server.");
return;
}
}
}
if (Config.ForwardMessages && 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;
await e.Channel.SendMessage(HelpCommand.HelpString);
await Task.Run(async () => {
await Task.Delay(2000);
repliedRecently = true;
2016-02-29 17:28:16 +00:00
});
} catch { }
}
2015-12-05 10:27:00 +00:00
}
2016-02-07 20:51:05 +00:00
}
//95520984584429568 meany