Skip to content

Commit

Permalink
Added Delete and Edit commands
Browse files Browse the repository at this point in the history
  • Loading branch information
byBlurr committed Jun 28, 2019
1 parent 64fe8e7 commit 52e8bdc
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 35 deletions.
107 changes: 94 additions & 13 deletions Globals/CommandModules/GlobalModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,99 @@ namespace Globals.CommandModules
{
public class GlobalModule : ModuleBase
{
[Command("edit")]
public async Task EditLastAsync([Remainder] string replacement = "")
{
var dbCon = DBConnection.Instance();
dbCon.DatabaseName = BotConfig.Load().DatabaseName;

if (dbCon.IsConnect())
{
string ChannelInUse = await Message.GetGlobalChannelInUseAsync(Context, dbCon);

if (!ChannelInUse.Equals(""))
{
if (!replacement.Equals("") && !ProfanityFilter.HasProfanity(replacement))
{
await Context.Message.DeleteAsync();

var messages = Message.GetMessageByUserAsync(Context.User, dbCon, ChannelInUse).Result;

var remove = Task.Run(async () =>
{
foreach (var message in messages)
{
if (message != null)
{
foreach (var embed in message.Embeds)
{
await (message as IUserMessage).ModifyAsync(x => x.Embed = embed.ToEmbedBuilder().WithDescription(replacement).Build());
await Task.Delay(1100);
}
}
}
});
}
else
{
var message = await Context.Channel.SendMessageAsync("Correct format of this command is `!edit <new text>`, for example `!edit Pokemon GO is my jam on toast.`.");
await Delete.DeleteMessage(message);
}
}
dbCon.Close();
}
}

[Command("delete")]
[Alias("del")]
public async Task DeleteLastAsync()
{
var dbCon = DBConnection.Instance();
dbCon.DatabaseName = BotConfig.Load().DatabaseName;

if (dbCon.IsConnect())
{
string ChannelInUse = await Message.GetGlobalChannelInUseAsync(Context, dbCon);

if (!ChannelInUse.Equals(""))
{

await Context.Message.DeleteAsync();

var messages = Message.GetMessageByUserAsync(Context.User, dbCon, ChannelInUse).Result;

var remove = Task.Run(async () =>
{
foreach (var message in messages)
{
if (message != null)
{
foreach (var embed in message.Embeds)
{
await (message as IUserMessage).ModifyAsync(x => x.Embed = embed.ToEmbedBuilder().WithDescription("").WithFooter("Removed by user at " + DateTime.UtcNow.ToString()).Build());
await Task.Delay(1100);
}
}
}
});
}
dbCon.Close();
}
}

[Command("request")]
public async Task RequestAsync([Remainder] string Request = "")
{
if (!Request.Equals(""))
var dbCon = DBConnection.Instance();
dbCon.DatabaseName = BotConfig.Load().DatabaseName;

if (dbCon.IsConnect())
{
var dbCon = DBConnection.Instance();
dbCon.DatabaseName = BotConfig.Load().DatabaseName;
if (dbCon.IsConnect())
{
string ChannelInUse = await Message.GetGlobalChannelInUseAsync(Context, dbCon);
string ChannelInUse = await Message.GetGlobalChannelInUseAsync(Context, dbCon);

if (!ChannelInUse.Equals(""))
if (!ChannelInUse.Equals(""))
{
if (!Request.Equals(""))
{
await Context.Message.DeleteAsync();

Expand All @@ -40,13 +121,13 @@ public async Task RequestAsync([Remainder] string Request = "")
var message = await Context.Channel.SendMessageAsync("Thank you for your suggestion, out team will now look into it and get back to you!");
await Delete.DeleteMessage(message);
}
dbCon.Close();
}
}
else
{
var message = await Context.Channel.SendMessageAsync("Correct format of this command is `!request <idea>`, for example `!request Pokemon GO global channel`.");
await Delete.DeleteMessage(message);
else
{
var message = await Context.Channel.SendMessageAsync("Correct format of this command is `!request <idea>`, for example `!request Pokemon GO global channel`.");
await Delete.DeleteMessage(message);
}
dbCon.Close();
}
}

Expand Down
91 changes: 69 additions & 22 deletions Globals/Global/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static async Task PostGlobalMessageAsync(SocketCommandContext Context)

public static async Task DeleteAsync(IUser User, DBConnection dbCon)
{
List<IMessage> MessagesToRemove = await GetMessagesByUser(User, dbCon);
List<IMessage> MessagesToRemove = await GetMessagesByUserAsync(User, dbCon);

var remove = Task.Run(async () =>
{
Expand Down Expand Up @@ -500,7 +500,7 @@ private static void CheckChannel(ref string message_channel, ICommandContext Con
}
}

private static async Task<List<IMessage>> GetMessagesByUser(IUser User, DBConnection dbCon)
public static async Task<List<IMessage>> GetMessagesByUserAsync(IUser User, DBConnection dbCon, int amount = 10)
{
string GId = await UserProfile.GetGlobalsIdAsync(User.Id, dbCon);

Expand All @@ -516,89 +516,89 @@ private static async Task<List<IMessage>> GetMessagesByUser(IUser User, DBConnec
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(2));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
var Messages = await Channel.GetMessagesAsync(amount).FlattenAsync();
AddMessagesToListAsync(Messages, GId, ref MessagesByUser);
}
}
if (reader.GetInt32(13) == 1)
{
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(3));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
var Messages = await Channel.GetMessagesAsync(amount).FlattenAsync();
AddMessagesToListAsync(Messages, GId, ref MessagesByUser);
}
}
if (reader.GetInt32(14) == 1)
{
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(4));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
var Messages = await Channel.GetMessagesAsync(amount).FlattenAsync();
AddMessagesToListAsync(Messages, GId, ref MessagesByUser);
}
}
if (reader.GetInt32(15) == 1)
{
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(5));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
var Messages = await Channel.GetMessagesAsync(amount).FlattenAsync();
AddMessagesToListAsync(Messages, GId, ref MessagesByUser);
}
}
if (reader.GetInt32(16) == 1)
{
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(6));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
var Messages = await Channel.GetMessagesAsync(amount).FlattenAsync();
AddMessagesToListAsync(Messages, GId, ref MessagesByUser);
}
}
if (reader.GetInt32(17) == 1)
{
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(7));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
var Messages = await Channel.GetMessagesAsync(amount).FlattenAsync();
AddMessagesToListAsync(Messages, GId, ref MessagesByUser);
}
}
if (reader.GetInt32(18) == 1)
{
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(8));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
var Messages = await Channel.GetMessagesAsync(amount).FlattenAsync();
AddMessagesToListAsync(Messages, GId, ref MessagesByUser);
}
}
if (reader.GetInt32(19) == 1)
{
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(9));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
var Messages = await Channel.GetMessagesAsync(amount).FlattenAsync();
AddMessagesToListAsync(Messages, GId, ref MessagesByUser);
}
}
if (reader.GetInt32(20) == 1)
{
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(10));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
var Messages = await Channel.GetMessagesAsync(amount).FlattenAsync();
AddMessagesToListAsync(Messages, GId, ref MessagesByUser);
}
}
if (reader.GetInt32(21) == 1)
{
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(11));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
var Messages = await Channel.GetMessagesAsync(amount).FlattenAsync();
AddMessagesToListAsync(Messages, GId, ref MessagesByUser);
}
}
}
Expand All @@ -607,6 +607,51 @@ private static async Task<List<IMessage>> GetMessagesByUser(IUser User, DBConnec
return MessagesByUser;
}

public static async Task<List<IMessage>> GetMessageByUserAsync(IUser User, DBConnection dbCon, string channel)
{
List<IMessage> MessagesByUser = new List<IMessage>();
string GId = await UserProfile.GetGlobalsIdAsync(User.Id, dbCon);

string query = "SELECT * FROM server_configs;";
var cmd = new MySqlCommand(query, dbCon.Connection);
var reader = await cmd.ExecuteReaderAsync();

while (await reader.ReadAsync())
{
if (channel == References.GamingChannel)
{
var Channel = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1)).GetTextChannel((ulong)reader.GetInt64(2));
if (Channel != null)
{
var Messages = await Channel.GetMessagesAsync(10).FlattenAsync();
AddMessageToListAsync(Messages, GId, ref MessagesByUser);
}
}
}

return MessagesByUser;
}

private static void AddMessagesToListAsync(IEnumerable<IMessage> Messages, string GId, ref List<IMessage> MessagesByUser)
{
foreach (IMessage message in Messages)
{
if (message != null)
{
foreach (IEmbed embed in message.Embeds)
{
if (embed.Footer.HasValue)
{
if (embed.Footer.Value.Text.Contains(GId))
{
MessagesByUser.Add(message);
}
}
}
}
}
}

private static void AddMessageToListAsync(IEnumerable<IMessage> Messages, string GId, ref List<IMessage> MessagesByUser)
{
foreach (IMessage message in Messages)
Expand All @@ -620,6 +665,8 @@ private static void AddMessageToListAsync(IEnumerable<IMessage> Messages, string
if (embed.Footer.Value.Text.Contains(GId))
{
MessagesByUser.Add(message);
// Return because we only want the first one...
return;
}
}
}
Expand Down

0 comments on commit 52e8bdc

Please sign in to comment.