2017-09-30 01:38:10 +02:00
|
|
|
|
using System;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
using System.Linq;
|
2017-09-30 17:26:24 +02:00
|
|
|
|
using System.Text;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2017-09-15 22:56:03 +02:00
|
|
|
|
using Discord;
|
2017-09-14 22:11:19 +02:00
|
|
|
|
using Discord.Commands;
|
2017-09-27 22:48:09 +02:00
|
|
|
|
using Discord.WebSocket;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
using Geekbot.net.Database;
|
|
|
|
|
using Geekbot.net.Database.Models;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.ErrorHandling;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
using Geekbot.net.Lib.Extensions;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Localization;
|
2017-09-14 22:11:19 +02:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
namespace Geekbot.net.Commands.Admin
|
2017-09-14 22:11:19 +02:00
|
|
|
|
{
|
|
|
|
|
[Group("admin")]
|
2017-10-27 00:18:58 +02:00
|
|
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
|
|
|
|
public class Admin : ModuleBase
|
2017-09-14 22:11:19 +02:00
|
|
|
|
{
|
2017-09-30 01:38:10 +02:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
private readonly DatabaseContext _database;
|
2017-11-15 01:08:20 +01:00
|
|
|
|
private readonly ITranslationHandler _translation;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-05-13 17:49:13 +02:00
|
|
|
|
public Admin(DatabaseContext database, DiscordSocketClient client, IErrorHandler errorHandler,
|
2017-12-29 01:53:50 +01:00
|
|
|
|
ITranslationHandler translationHandler)
|
2017-09-14 22:11:19 +02:00
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
_database = database;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
_client = client;
|
|
|
|
|
_errorHandler = errorHandler;
|
2017-11-15 01:08:20 +01:00
|
|
|
|
_translation = translationHandler;
|
2017-09-14 22:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Command("welcome", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Set a Welcome Message (use '$user' to mention the new joined user).")]
|
|
|
|
|
public async Task SetWelcomeMessage([Remainder] [Summary("message")] string welcomeMessage)
|
2017-09-14 22:11:19 +02:00
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var guild = GetGuildSettings(Context.Guild.Id);
|
|
|
|
|
guild.WelcomeMessage = welcomeMessage;
|
|
|
|
|
_database.GuildSettings.Update(guild);
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
2018-05-13 17:49:13 +02:00
|
|
|
|
|
2017-09-14 22:11:19 +02:00
|
|
|
|
var formatedMessage = welcomeMessage.Replace("$user", Context.User.Mention);
|
2018-03-02 23:05:14 +01:00
|
|
|
|
await ReplyAsync($"Welcome message has been changed\r\nHere is an example of how it would look:\r\n{formatedMessage}");
|
2017-09-14 22:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-30 17:26:24 +02:00
|
|
|
|
[Command("modchannel", RunMode = RunMode.Async)]
|
2017-10-12 16:34:10 +02:00
|
|
|
|
[Summary("Set a channel for moderation purposes")]
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public async Task SelectModChannel([Summary("#Channel")] ISocketMessageChannel channel)
|
2017-09-30 17:26:24 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var m = await channel.SendMessageAsync("verifying...");
|
|
|
|
|
|
|
|
|
|
var guild = GetGuildSettings(Context.Guild.Id);
|
|
|
|
|
guild.ModChannel = channel.Id.AsLong();
|
|
|
|
|
_database.GuildSettings.Update(guild);
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
2018-05-13 17:49:13 +02:00
|
|
|
|
|
2017-09-30 17:26:24 +02:00
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
sb.AppendLine("Successfully saved mod channel, you can now do the following");
|
2018-05-13 17:49:13 +02:00
|
|
|
|
sb.AppendLine("- `!admin showleave` - send message to mod channel when someone leaves");
|
|
|
|
|
sb.AppendLine("- `!admin showdel` - send message to mod channel when someone deletes a message");
|
|
|
|
|
await m.ModifyAsync(e => e.Content = sb.ToString());
|
2017-09-30 17:26:24 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to be valid");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Command("showleave", RunMode = RunMode.Async)]
|
2018-05-13 17:49:13 +02:00
|
|
|
|
[Summary("Toggle - notify modchannel when someone leaves")]
|
|
|
|
|
public async Task ShowLeave()
|
2017-09-30 17:26:24 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var guild = GetGuildSettings(Context.Guild.Id);
|
|
|
|
|
var modChannel = await GetModChannel(guild.ModChannel.AsUlong());
|
|
|
|
|
if (modChannel == null) return;
|
|
|
|
|
|
|
|
|
|
guild.ShowLeave = !guild.ShowLeave;
|
|
|
|
|
_database.GuildSettings.Update(guild);
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
2018-05-13 17:49:13 +02:00
|
|
|
|
await modChannel.SendMessageAsync(guild.ShowLeave
|
|
|
|
|
? "Saved - now sending messages here when someone leaves"
|
|
|
|
|
: "Saved - stopping sending messages here when someone leaves"
|
|
|
|
|
);
|
2017-09-30 17:26:24 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
2017-09-30 17:26:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-09-30 17:26:24 +02:00
|
|
|
|
[Command("showdel", RunMode = RunMode.Async)]
|
2018-05-13 17:49:13 +02:00
|
|
|
|
[Summary("Toggle - notify modchannel when someone deletes a message")]
|
|
|
|
|
public async Task ShowDelete()
|
2017-09-30 17:26:24 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var guild = GetGuildSettings(Context.Guild.Id);
|
|
|
|
|
var modChannel = await GetModChannel(guild.ModChannel.AsUlong());
|
|
|
|
|
if (modChannel == null) return;
|
|
|
|
|
|
|
|
|
|
guild.ShowDelete = !guild.ShowDelete;
|
|
|
|
|
_database.GuildSettings.Update(guild);
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
2018-05-13 17:49:13 +02:00
|
|
|
|
await modChannel.SendMessageAsync(guild.ShowDelete
|
|
|
|
|
? "Saved - now sending messages here when someone deletes a message"
|
|
|
|
|
: "Saved - stopping sending messages here when someone deletes a message"
|
|
|
|
|
);
|
2017-09-30 17:26:24 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
2017-09-30 01:38:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-11-15 01:08:20 +01:00
|
|
|
|
[Command("setlang", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Change the bots language")]
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public async Task SetLanguage([Summary("language")] string languageRaw)
|
2017-11-15 01:08:20 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var language = languageRaw.ToUpper();
|
|
|
|
|
var success = _translation.SetLanguage(Context.Guild.Id, language);
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var guild = GetGuildSettings(Context.Guild.Id);
|
|
|
|
|
guild.Language = language;
|
|
|
|
|
_database.GuildSettings.Update(guild);
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
2018-05-13 17:49:13 +02:00
|
|
|
|
|
2017-11-16 00:51:36 +01:00
|
|
|
|
var trans = _translation.GetDict(Context);
|
2017-11-16 17:19:43 +01:00
|
|
|
|
await ReplyAsync(trans["NewLanguageSet"]);
|
2017-11-15 01:08:20 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-11-15 01:08:20 +01:00
|
|
|
|
await ReplyAsync(
|
|
|
|
|
$"That doesn't seem to be a supported language\r\nSupported Languages are {string.Join(", ", _translation.GetSupportedLanguages())}");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-28 17:38:45 +02:00
|
|
|
|
|
|
|
|
|
[Command("wiki", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Change the wikipedia instance (use lang code in xx.wikipedia.org)")]
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public async Task SetWikiLanguage([Summary("language")] string languageRaw)
|
2018-04-28 17:38:45 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var language = languageRaw.ToLower();
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var guild = GetGuildSettings(Context.Guild.Id);
|
|
|
|
|
guild.WikiLang = language;
|
|
|
|
|
_database.GuildSettings.Update(guild);
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
2018-05-13 17:49:13 +02:00
|
|
|
|
|
2018-04-28 17:38:45 +02:00
|
|
|
|
await ReplyAsync($"Now using the {language} wikipedia");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-13 17:49:13 +02:00
|
|
|
|
|
|
|
|
|
[Command("ping", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Enable the ping reply.")]
|
|
|
|
|
public async Task TogglePing()
|
2017-11-16 00:51:36 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var guild = GetGuildSettings(Context.Guild.Id);
|
|
|
|
|
guild.Ping = !guild.Ping;
|
|
|
|
|
_database.GuildSettings.Update(guild);
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
2018-05-13 17:49:13 +02:00
|
|
|
|
await ReplyAsync(guild.Ping ? "i will reply to ping now" : "No more pongs...");
|
2017-11-16 00:51:36 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-02 23:05:14 +01:00
|
|
|
|
|
2018-05-13 17:49:13 +02:00
|
|
|
|
[Command("hui", RunMode = RunMode.Async)]
|
2018-03-02 23:05:14 +01:00
|
|
|
|
[Summary("Enable the ping reply.")]
|
2018-05-13 17:49:13 +02:00
|
|
|
|
public async Task ToggleHui()
|
2018-03-02 23:05:14 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
var guild = GetGuildSettings(Context.Guild.Id);
|
|
|
|
|
guild.Hui = !guild.Hui;
|
|
|
|
|
_database.GuildSettings.Update(guild);
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
2018-05-13 17:49:13 +02:00
|
|
|
|
await ReplyAsync(guild.Hui ? "i will reply to hui now" : "No more hui's...");
|
2018-03-02 23:05:14 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-13 17:49:13 +02:00
|
|
|
|
|
|
|
|
|
private GuildSettingsModel GetGuildSettings(ulong guildId)
|
|
|
|
|
{
|
|
|
|
|
var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong()));
|
|
|
|
|
if (guild != null) return guild;
|
|
|
|
|
Console.WriteLine("Adding non-exist Guild Settings to database");
|
|
|
|
|
_database.GuildSettings.Add(new GuildSettingsModel()
|
|
|
|
|
{
|
|
|
|
|
GuildId = guildId.AsLong(),
|
|
|
|
|
Hui = false,
|
|
|
|
|
Ping = false,
|
|
|
|
|
Language = "en",
|
|
|
|
|
ShowDelete = false,
|
|
|
|
|
ShowLeave = false,
|
|
|
|
|
WikiLang = "en"
|
|
|
|
|
});
|
|
|
|
|
_database.SaveChanges();
|
|
|
|
|
return _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<ISocketMessageChannel> GetModChannel(ulong channelId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if(channelId == ulong.MinValue) throw new Exception();
|
|
|
|
|
var modChannel = (ISocketMessageChannel) _client.GetChannel(channelId);
|
|
|
|
|
if(modChannel == null) throw new Exception();
|
|
|
|
|
return modChannel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync(
|
|
|
|
|
"Modchannel doesn't seem to exist, please set one with `!admin modchannel [channelId]`");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-02 23:05:14 +01:00
|
|
|
|
|
2017-09-14 22:11:19 +02:00
|
|
|
|
}
|
2017-04-14 22:18:22 +02:00
|
|
|
|
}
|