Files
EventDiscordBot/EventBot/Modules/BasicModule.cs
2019-08-01 20:25:28 +03:00

114 lines
4.2 KiB
C#

using Discord;
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using EventBot.Attributes;
using EventBot.Services;
using Discord.Addons.Interactive;
using EventBot.Misc;
namespace EventBot.Modules
{
public class BasicModule : ModuleBase<SocketCommandContext>
{
private readonly DatabaseService _database;
public BasicModule(DatabaseService database)
{
_database = database;
}
[RequireUserPermission(GuildPermission.Administrator, Group = "Permission")]
[RequireOwner(Group = "Permission")]
[RequireContext(ContextType.Guild)]
[Command("prefix")]
[Name("Configure prefix")]
[Summary("Gets prefix.")]
public async Task PrefixCommand()
{
var guildConfig = _database.GuildConfigs.FirstOrDefault(g => g.GuildId == Context.Guild.Id);
if (guildConfig == null)
throw new Exception("No guild config was foumd.");
if(guildConfig.Prefix != null)
await ReplyAsync($"Current prefix is `{guildConfig.Prefix}`");
else
await ReplyAsync($"There is no prefix set for this guild.");
}
[RequireUserPermission(GuildPermission.Administrator, Group = "Permission")]
[RequireOwner(Group = "Permission")]
[RequireContext(ContextType.Guild)]
[Command("prefix")]
[Name("Configure prefix")]
[Summary("Sets prefix.")]
public async Task PrefixCommand(
[Summary("New prefix to set")] string newPrefix)
{
var guildConfig = _database.GuildConfigs.FirstOrDefault(g => g.GuildId == Context.Guild.Id);
if (guildConfig == null)
throw new Exception("No guild config was foumd.");
guildConfig.Prefix = newPrefix;
await _database.SaveChangesAsync();
await ReplyAsync($"Prefix has been set to `{guildConfig.Prefix}`");
}
[Group("help")]
public class HelpModule : InteractiveBase<SocketCommandContext>
{
private readonly CommandService _commands;
public HelpModule(CommandService commands)
{
_commands = commands;
}
[Command]
[Name("Help")]
[Summary("Lists all commands with there descriptions.")]
public async Task DefaultHelpAsync()
{
var pagedCommands = _commands.Commands
.OrderBy(c => c.Aliases[0])
.Where(c => c.Attributes
.Select(a => {
switch (a)
{
case NoHelpAttribute _:
return false;
case RequireContextAttribute rc:
return Context.IsContextType(rc.Contexts);
default:
return true;
}
})
.Aggregate(true, (a, r) => a && r))
.SelectMany(c => c.Aliases.Select(a => new { CI = c, MA = (c.Aliases[0] == a), A = a}).Reverse())
.Select((e, i) => new { Command = e, Index = i })
.GroupBy(o => o.Index / 20)
.Select(g => g.Select(o => o.Command));
var pager = new PaginatedMessage()
{
Title = "Command list",
Color = Color.DarkBlue,
Pages = pagedCommands.Select(p => string.Join("\r\n", p.Select(c => {
if (c.MA)
return $"`{c.CI.FormatCallHelpString()}` - **{c.CI.Name}**";
else
return $"`{c.A}` `↪`";
}))),
Options = new PaginatedAppearanceOptions()
{
Info = null,
JumpDisplayOptions = JumpDisplayOptions.Never
}
};
await PagedReplyAsync(pager);
}
}
}
}