Skip to content

Commit

Permalink
Small optimization
Browse files Browse the repository at this point in the history
Cleaned up some code.
Moved sending messages onto a seperate thread.
Added a delay of 1 millisecond between each message sent.
Will still hit the limit when in multiple servers but we will see...
  • Loading branch information
byBlurr committed Jun 28, 2019
1 parent 576c09d commit d0ca5f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 151 deletions.
4 changes: 2 additions & 2 deletions Globals/CommandModules/GlobalModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public async Task ProfileIdAsync(int ID = -1)
var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
await Message.PostMessageAsync(ChannelInUse, reader, embed);
await Message.PostToChannelAsync(ChannelInUse, reader, embed);
}
}
dbCon.Close();
Expand Down Expand Up @@ -216,7 +216,7 @@ public async Task ProfileAsync(IUser User = null)
var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
await Message.PostMessageAsync(ChannelInUse, reader, embed);
await Message.PostToChannelAsync(ChannelInUse, reader, embed);
}
}
dbCon.Close();
Expand Down
190 changes: 41 additions & 149 deletions Globals/Global/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static async Task PostGlobalMessageAsync(SocketCommandContext Context)
{
if (!user_rank.ToLower().Equals("blacklisted"))
{
await PostMessageAsync(message_channel, reader, embed, message_images);
await PostToChannelAsync(message_channel, reader, embed, message_images);
}
}

Expand Down Expand Up @@ -153,7 +153,7 @@ public static async Task<string> GetGlobalChannelInUseAsync(ICommandContext Cont
return message_channel;
}

public static async Task PostMessageAsync(string message_channel, DbDataReader reader, EmbedBuilder embed, IReadOnlyCollection<Attachment> message_images = null)
public static async Task PostToChannelAsync(string message_channel, DbDataReader reader, EmbedBuilder embed, IReadOnlyCollection<Attachment> message_images = null)
{
if (message_channel.Equals(References.GamingChannel))
{
Expand All @@ -162,30 +162,7 @@ public static async Task PostMessageAsync(string message_channel, DbDataReader r

if (Enabled == 1)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
if (message_images != null)
{
if (message_images.Count > 0)
{
foreach (var attachment in message_images)
{
//await (channel as IMessageChannel).SendFileAsync(attachment.Url);
}
}
}
var message = await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
}
else
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
}
}
await PostMessageAsync(message_channel, reader, embed, message_images, Id);
}
}
else if (message_channel.Equals(References.MusicChannel))
Expand All @@ -195,20 +172,7 @@ public static async Task PostMessageAsync(string message_channel, DbDataReader r

if (Enabled == 1)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
}
else
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
}
}
await PostMessageAsync(message_channel, reader, embed, message_images, Id);
}
}
else if (message_channel.Equals(References.MovieChannel))
Expand All @@ -218,20 +182,7 @@ public static async Task PostMessageAsync(string message_channel, DbDataReader r

if (Enabled == 1)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
}
else
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
}
}
await PostMessageAsync(message_channel, reader, embed, message_images, Id);
}
}
else if (message_channel.Equals(References.R6Channel))
Expand All @@ -241,20 +192,7 @@ public static async Task PostMessageAsync(string message_channel, DbDataReader r

if (Enabled == 1)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
}
else
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
}
}
await PostMessageAsync(message_channel, reader, embed, message_images, Id);
}
}
else if (message_channel.Equals(References.LeagueChannel))
Expand All @@ -264,20 +202,7 @@ public static async Task PostMessageAsync(string message_channel, DbDataReader r

if (Enabled == 1)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
}
else
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
}
}
await PostMessageAsync(message_channel, reader, embed, message_images, Id);
}
}
else if (message_channel.Equals(References.RustChannel))
Expand All @@ -287,20 +212,7 @@ public static async Task PostMessageAsync(string message_channel, DbDataReader r

if (Enabled == 1)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
}
else
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
}
}
await PostMessageAsync(message_channel, reader, embed, message_images, Id);
}
}
else if (message_channel.Equals(References.GtaChannel))
Expand All @@ -310,20 +222,7 @@ public static async Task PostMessageAsync(string message_channel, DbDataReader r

if (Enabled == 1)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
}
else
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
}
}
await PostMessageAsync(message_channel, reader, embed, message_images, Id);
}
}
else if (message_channel.Equals(References.PubgChannel))
Expand All @@ -333,20 +232,7 @@ public static async Task PostMessageAsync(string message_channel, DbDataReader r

if (Enabled == 1)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
}
else
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
}
}
await PostMessageAsync(message_channel, reader, embed, message_images, Id);
}
}
else if (message_channel.Equals(References.FortniteChannel))
Expand All @@ -356,20 +242,7 @@ public static async Task PostMessageAsync(string message_channel, DbDataReader r

if (Enabled == 1)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
}
else
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
}
}
await PostMessageAsync(message_channel, reader, embed, message_images, Id);
}
}
else if (message_channel.Equals(References.ApexChannel))
Expand All @@ -379,20 +252,39 @@ public static async Task PostMessageAsync(string message_channel, DbDataReader r

if (Enabled == 1)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
await PostMessageAsync(message_channel, reader, embed, message_images, Id);
}
}
}

private static async Task PostMessageAsync(string message_channel, DbDataReader reader, EmbedBuilder embed, IReadOnlyCollection<Attachment> message_images, ulong Id)
{
var guild = CommandHandler.GetBot().GetGuild((ulong)reader.GetInt64(1));
if (guild != null)
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
var send = Task.Run(async () =>
{
var channel = guild.GetChannel(Id);
if (channel != null)
{
await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
}
else
if (message_images != null)
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
if (message_images.Count > 0)
{
foreach (var attachment in message_images)
{
//await (channel as IMessageChannel).SendFileAsync(attachment.Url);
}
}
}
}
var message = await (channel as IMessageChannel).SendMessageAsync(null, false, embed.Build());
await Task.Delay(1);
});
}
else
{
var message = await guild.Owner.SendMessageAsync("It appears there is an issue with finding the '" + message_channel + "` channel on your server. Try deleting the servers and then running the create command again.");
await Delete.DeleteMessage(message);
}
}
}
Expand Down

0 comments on commit d0ca5f4

Please sign in to comment.