Skip to content

Commit

Permalink
Added Cooldown attribute
Browse files Browse the repository at this point in the history
Added the command 'purge'
  • Loading branch information
Voltstro committed Jul 29, 2019
1 parent 390c1b6 commit a128e1f
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Pootis-Bot/Core/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ private async Task HandleCommandAsync(SocketMessage messageParam)
var result = await _commands.ExecuteAsync(context, argPos, services: null);
if (!result.IsSuccess && result.Error == CommandError.BadArgCount)
await context.Channel.SendMessageAsync($"The command `{msg.Content.Replace(Global.botPrefix, "")}` either has too many or too little arguments!");
else if (!result.IsSuccess && result.Error == CommandError.UnmetPrecondition)
await context.Channel.SendMessageAsync(result.ErrorReason);
else if (!result.IsSuccess && result.Error != CommandError.UnknownCommand)
Global.Log(result.ErrorReason, ConsoleColor.Red);
}
Expand Down
2 changes: 1 addition & 1 deletion Pootis-Bot/Core/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static class Global
// Main Server - Development Server
internal static readonly string[] discordServers = { "https://discord.creepysin.com", "https://discord.gg/m4YcsUa" };

internal static readonly string version = "0.2.7";
internal static readonly string version = "0.2.8";
internal static readonly string aboutMessage = $"Pootis Bot --- | --- {version}\n" +
$"Created by Creepysin licensed under the MIT license. Vist {githubPage}/blob/master/LICENSE.md for more info.\n\n" +
$"Pootis Robot icon by Valve\n" +
Expand Down
6 changes: 6 additions & 0 deletions Pootis-Bot/Entities/GlobalServerList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public struct VoiceChannel
{
public ulong ID { get; set; }
public string Name { get; set;}

public VoiceChannel(ulong id, string name)
{
ID = id;
Name = name;
}
}

public class CommandInfo
Expand Down
3 changes: 3 additions & 0 deletions Pootis-Bot/Modules/Audio/Music.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Discord;
using Discord.Commands;
using Pootis_Bot.Core;
using Pootis_Bot.Preconditions;
using Pootis_Bot.Services;

namespace Pootis_Bot.Modules.Audio
Expand All @@ -22,6 +23,7 @@ public Music()

[Command("join", RunMode = RunMode.Async)]
[Summary("Joins in the current voice channel you are in")]
[Cooldown(5)]
[RequireBotPermission(GuildPermission.Connect)]
[RequireBotPermission(GuildPermission.Speak)]
public async Task JoinCmd()
Expand Down Expand Up @@ -50,6 +52,7 @@ public async Task LeaveCmd()

[Command("play", RunMode = RunMode.Async)]
[Summary("Plays a song")]
[Cooldown(5)]
[RequireBotPermission(GuildPermission.Speak)]
public async Task PlayCmd([Remainder] string song = "")
{
Expand Down
8 changes: 3 additions & 5 deletions Pootis-Bot/Modules/Audio/VoiceChannels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Discord.Rest;
using Pootis_Bot.Core;
using Pootis_Bot.Entities;
using Pootis_Bot.Preconditions;

namespace Pootis_Bot.Modules.Audio
{
Expand All @@ -14,17 +15,14 @@ public class VoiceChannels : ModuleBase<ICommandContext>
[RequireBotPermission(GuildPermission.ManageChannels)]
[RequireBotPermission(GuildPermission.MoveMembers)]
[RequireUserPermission(GuildPermission.ManageChannels)]
[Cooldown(5)]
public async Task AddVoiceChannel(string baseName)
{
RestVoiceChannel channel = await (Context.Guild as SocketGuild).CreateVoiceChannelAsync($"➕ New {baseName} VC");

await Context.Channel.SendMessageAsync($"Added {baseName} as an auto voice channel.");

GlobalServerList.VoiceChannel voiceChannel = new GlobalServerList.VoiceChannel
{
ID = channel.Id,
Name = baseName
};
GlobalServerList.VoiceChannel voiceChannel = new GlobalServerList.VoiceChannel(channel.Id, baseName);

ServerLists.GetServer((SocketGuild)Context.Guild).VoiceChannels.Add(voiceChannel);
ServerLists.SaveServerList();
Expand Down
3 changes: 3 additions & 0 deletions Pootis-Bot/Modules/Basic/Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Discord.Commands;
using Discord.WebSocket;
using Pootis_Bot.Core;
using Pootis_Bot.Preconditions;
using Pootis_Bot.Entities;

namespace Pootis_Bot.Modules.Basic
Expand All @@ -18,6 +19,7 @@ public class BasicCommands : ModuleBase<SocketCommandContext>

[Command("hello")]
[Summary("Displays the 'hello' message")]
[Cooldown(5)]
public async Task Hello()
{
var embed = new EmbedBuilder();
Expand All @@ -37,6 +39,7 @@ public async Task Hello()

[Command("server")]
[Summary("Gets details about the server you are in")]
[Cooldown(5)]
public async Task ServerGuild()
{
var guilduser = (SocketGuildUser)Context.User;
Expand Down
16 changes: 16 additions & 0 deletions Pootis-Bot/Modules/Server/ServerAdminCommands.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System.Threading.Tasks;

namespace Pootis_Bot.Modules.Server
Expand Down Expand Up @@ -30,5 +31,20 @@ public async Task BanUser(IGuildUser user, int days = 0, [Remainder]string reaso
await user.BanAsync(days, reason);
await Context.Channel.SendMessageAsync($"The user {user.Username} was banned");
}

[Command("purge", RunMode = RunMode.Async)]
[Summary("Deletes bulk messages")]
[RequireBotPermission(GuildPermission.ManageMessages)]
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task Purge(int messageCount = 10)
{
var messages = Context.Channel.GetMessagesAsync(messageCount + 1).FlattenAsync();

await (Context.Channel as SocketTextChannel).DeleteMessagesAsync(messages.Result);

var message = await Context.Channel.SendMessageAsync($"{messageCount} message were deleted, the message will be deleted in a moment.");
await Task.Delay(3000);
await message.DeleteAsync();
}
}
}
53 changes: 53 additions & 0 deletions Pootis-Bot/Preconditions/CooldownAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Discord.Commands;

namespace Pootis_Bot.Preconditions
{
public class CooldownAttribute : PreconditionAttribute
{
TimeSpan CooldownLength { get; set; }
readonly ConcurrentDictionary<CooldownInfo, DateTime> _cooldowns = new ConcurrentDictionary<CooldownInfo, DateTime>();

public CooldownAttribute(int seconds)
{
CooldownLength = TimeSpan.FromSeconds(seconds);
}

public struct CooldownInfo
{
public ulong UserId { get; }
public int CommandHashCode { get; }

public CooldownInfo(ulong userId, int commandHashCode)
{
UserId = userId;
CommandHashCode = commandHashCode;
}
}

public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{
var key = new CooldownInfo(context.User.Id, command.GetHashCode());

if (_cooldowns.TryGetValue(key, out DateTime endsAt))
{
var difference = endsAt.Subtract(DateTime.Now);
if (difference.Ticks > 0)
{
return Task.FromResult(PreconditionResult.FromError($"Please wait {difference.ToString(@"ss")} seconds before trying again!"));
}

var time = DateTime.Now.Add(CooldownLength);
_cooldowns.TryUpdate(key, time, endsAt);
}
else
{
_cooldowns.TryAdd(key, DateTime.Now.Add(CooldownLength));
}

return Task.FromResult(PreconditionResult.FromSuccess());
}
}
}
5 changes: 1 addition & 4 deletions Pootis-Bot/Preconditions/GuildOwnerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Pootis_Bot.Preconditions
{
//
// Summary:
// Requires the command to be invoked by the owner of the Discord server.
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class RequireGuildOwnerAttribute : PreconditionAttribute
{
Expand All @@ -15,7 +12,7 @@ public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext c
if (context.User.Id == context.Guild.OwnerId)
return Task.FromResult(PreconditionResult.FromSuccess());
else
return Task.FromResult(PreconditionResult.FromError("You are not the owner of this Discord server!"));
return Task.FromResult(PreconditionResult.FromError("You are not the owner of this Discord server, you cannot run this command!"));
}
}
}

0 comments on commit a128e1f

Please sign in to comment.