Removed all local copies of Random rnd in favor of using Program.rnd. Fixed lack of DM command usage. Coverted more responses to use DiscordEmbedBuilder

This commit is contained in:
Christopher Darnell committed 2021-04-29 05:02:17 -07:00
1 parent 0479d443b8
commit 7cefceb48e
10 files changed
+115 -86

No files matched your search

-2
View File
@@ -18,8 +18,6 @@ namespace StarsiegeBot
class BotSettings : BaseCommandModule
{
public static Dictionary<string, GuildSettings> GuildSettings;
public BotSettings()
{
Console.WriteLine("Bot Setting Commands Loaded");
+30 -21
View File
@@ -12,9 +12,6 @@ namespace StarsiegeBot
{
class Commands : BaseCommandModule
{
// this class depends a lot on the random stuff. so make it class level.
private readonly Random rnd = new Random();
// Master insult list.
private readonly string[] InsultMaster = {
"{0} slaps {1} around a bit with a large trout.",
@@ -41,8 +38,8 @@ namespace StarsiegeBot
// give the end user the randomly choosen result.
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = results[rnd.Next(0, results.Length)],
Color = Program.colours[rnd.Next(0, Program.colours.Length)]
Description = results[Program.rnd.Next(0, results.Length)],
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
};
await ctx.RespondAsync(embed);
}
@@ -65,7 +62,7 @@ namespace StarsiegeBot
embed.AddField("Ping", ctx.Client.Ping.ToString(), true);
embed.AddField("Shard Count", ctx.Client.ShardCount.ToString(), true);
embed.AddField("Shard", ctx.Client.ShardId.ToString(), true);
embed.Color = Program.colours[rnd.Next(0, Program.colours.Length)];
embed.Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)];
// list all the owners of the bot.
string owners = "";
foreach (DiscordUser owner in ctx.Client.CurrentApplication.Owners)
@@ -86,8 +83,8 @@ namespace StarsiegeBot
string[] choices = { "heads", "tails" };
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = "The coin landed on... " + choices[rnd.Next(0, choices.Length)],
Color = Program.colours[rnd.Next(0, Program.colours.Length)]
Description = "The coin landed on... " + choices[Program.rnd.Next(0, choices.Length)],
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
};
await ctx.RespondAsync(embed);
}
@@ -153,7 +150,7 @@ namespace StarsiegeBot
// this is the result.
// This line looks weird, I know, so here is what is going on. If the number of sides is negative, we adjust for that.
// since the rnd.Next is inclusive, exclusive, we have to adjust for that.
int roll = rnd.Next(Math.Min(1, sides), Math.Max(sides + 1, 0));
int roll = Program.rnd.Next(Math.Min(1, sides), Math.Max(sides + 1, 0));
// do we have our first result? No, make it! Store the results in a string to show the end user.
if (dieResults.Equals(String.Empty))
dieResults = roll.ToString();
@@ -177,6 +174,18 @@ namespace StarsiegeBot
else
await ctx.RespondAsync(overallResults);
}
[Command("guilds"),Hidden,RequireOwner]
public async Task GimmeServerNames (CommandContext ctx)
{
string output = "";
foreach (KeyValuePair<ulong,DiscordGuild> item in ctx.Client.Guilds)
{
output += item.Value.Name + "\r\n";
}
await ctx.RespondAsync(output);
}
[Command("random")]
[Description("Random number generator.")]
public async Task Random(CommandContext ctx, [Description("Lowest number choice. (Inclusive)")] int min, [Description("Highest Number Choice. (Inclusive)")] int max)
@@ -189,8 +198,8 @@ namespace StarsiegeBot
// output the results
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = $"🎲 Your random number is: {rnd.Next(i, a)}",
Color = Program.colours[rnd.Next(0, Program.colours.Length)]
Description = $"🎲 Your random number is: {Program.rnd.Next(i, a)}",
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
};
await ctx.RespondAsync(embed);
}
@@ -202,7 +211,7 @@ namespace StarsiegeBot
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = $"Gives you a random number between <Option 1> and <Option 2> Try someething like `random 3 30`!",
Color = Program.colours[rnd.Next(0, Program.colours.Length)]
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
};
await ctx.RespondAsync(embed);
}
@@ -216,7 +225,7 @@ namespace StarsiegeBot
// --- THIS FUNCTION IS A COPY AND PASTE OF RPSLS. AND ALL LOGIC IS IDENTICAL TO IT. PLEASE REFER TO THAT METHOD. --- */
string[] choices = new string[] { "rock", "paper", "scissors" };
text = text.ToLower();
string botChoice = choices[rnd.Next(0, choices.Length)];
string botChoice = choices[Program.rnd.Next(0, choices.Length)];
string result;
if (text.Equals(""))
@@ -237,7 +246,7 @@ namespace StarsiegeBot
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = result,
Color = Program.colours[rnd.Next(0, Program.colours.Length)]
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
};
await ctx.RespondAsync(embed);
}
@@ -267,7 +276,7 @@ namespace StarsiegeBot
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = result,
Color = Program.colours[rnd.Next(0, Program.colours.Length)]
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
};
await ctx.RespondAsync(embed);
}
@@ -282,7 +291,7 @@ namespace StarsiegeBot
// lets work in all lower case letters. makes life easier.
text = text.ToLower();
// make a choice for the bot.
string botChoice = choices[rnd.Next(0, choices.Length)];
string botChoice = choices[Program.rnd.Next(0, choices.Length)];
// create a result string.
string result;
@@ -312,7 +321,7 @@ namespace StarsiegeBot
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = result,
Color = Program.colours[rnd.Next(0, Program.colours.Length)]
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
};
await ctx.RespondAsync(embed);
}
@@ -355,7 +364,7 @@ namespace StarsiegeBot
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = result,
Color = Program.colours[rnd.Next(0, Program.colours.Length)]
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
};
await ctx.RespondAsync(embed);
}
@@ -377,7 +386,7 @@ namespace StarsiegeBot
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = result,
Color = Program.colours[rnd.Next(0, Program.colours.Length)]
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
};
await ctx.RespondAsync(embed);
}
@@ -403,12 +412,12 @@ namespace StarsiegeBot
user2 = user.Mention;
}
// pick a random insult, and populate the names correctly.
int randInt = rnd.Next(0, InsultCopy.Count);
int randInt = Program.rnd.Next(0, InsultCopy.Count);
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = string.Format(InsultCopy[randInt], user1, user2),
Color = Program.colours[rnd.Next(0, Program.colours.Length)]
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
};
await ctx.RespondAsync(embed);
InsultCopy.RemoveAt(randInt);
+29 -38
View File
@@ -58,16 +58,11 @@ namespace StarsiegeBot
// Check if message has valid prefix.
// json file loaded...
int cmdStart = msg.GetStringPrefixLength("!");
// if Guild is null, do nothing for now.
if (e.Guild is null)
{
}
else
List<string> prefixes = BotSettings.GuildSettings["default"].Prefixes;
prefixes.Add(d.CurrentUser.Mention);
if (!(e.Guild is null))
{
string gId = e.Guild.Id.ToString();
List<string> prefixes = BotSettings.GuildSettings["default"].Prefixes;
// Check to see if the guild has settings.
if (BotSettings.GuildSettings.ContainsKey(gId))
{
@@ -83,27 +78,25 @@ namespace StarsiegeBot
prefixes = BotSettings.GuildSettings[gId].Prefixes;
}
}
// check each prefix. break on the one that is evoked.
foreach (string item in prefixes)
{
cmdStart = msg.GetStringPrefixLength(item);
if (cmdStart != -1) break;
}
// we didn't find a command prefix... Break.
if (cmdStart == -1) return Task.CompletedTask;
// Retrieve prefix.
var prefix = msg.Content.Substring(0, cmdStart);
var cmdString = msg.Content[cmdStart..];
// Retrieve full command string.
var command = cnext.FindCommand(cmdString, out var args);
if (command == null) return Task.CompletedTask;
var ctx = cnext.CreateContext(msg, prefix, command, args);
Task.Run(async () => await cnext.ExecuteCommandAsync(ctx));
}
foreach (string item in prefixes)
{
cmdStart = msg.GetStringPrefixLength(item);
if (cmdStart != -1) break;
}
// we didn't find a command prefix... Break.
if (cmdStart == -1) return Task.CompletedTask;
// Retrieve prefix.
var prefix = msg.Content.Substring(0, cmdStart);
var cmdString = msg.Content[cmdStart..];
// Retrieve full command string.
var command = cnext.FindCommand(cmdString, out var args);
if (command == null) return Task.CompletedTask;
var ctx = cnext.CreateContext(msg, prefix, command, args);
Task.Run(async () => await cnext.ExecuteCommandAsync(ctx));
return Task.CompletedTask;
}
@@ -231,7 +224,9 @@ namespace StarsiegeBot
public Task GuildDownloadCompleted(DiscordClient d, GuildDownloadCompletedEventArgs e)
{
d.Logger.LogDebug(BotEventId, "GuildDownloadCompleted.");
notify = e.Guilds[376937422010974209].Members[139548200099905536];
//if(e.Guilds[376937422010974209].Members.ContainsKey(139548200099905536))
//notify = e.Guilds[376937422010974209].Members[139548200099905536];
return Task.CompletedTask;
}
public Task GuildEmojisUpdated(DiscordClient d, GuildEmojisUpdateEventArgs e)
@@ -253,16 +248,12 @@ namespace StarsiegeBot
}
string gId = e.Guild.Id.ToString();
if (BotSettings.GuildSettings[gId].UseWelcome)
if (BotSettings.GuildSettings[gId].UseWelcome &&
!(BotSettings.GuildSettings[gId].WelcomeChannel is null) &&
!(BotSettings.GuildSettings[gId].WelcomeMessage is null))
{
if (!(BotSettings.GuildSettings[gId].WelcomeChannel is null))
{
if (!(BotSettings.GuildSettings[gId].WelcomeMessage is null))
{
string msg = WelcomeMessage.WelcomeMessageProcessing(BotSettings.GuildSettings[gId].WelcomeMessage, e.Member, e.Guild);
BotSettings.GuildSettings[gId].WelcomeChannel.SendMessageAsync(msg);
}
}
string msg = WelcomeMessage.WelcomeMessageProcessing(BotSettings.GuildSettings[gId].WelcomeMessage, e.Member, e.Guild);
BotSettings.GuildSettings[gId].WelcomeChannel.SendMessageAsync(msg);
}
return Task.CompletedTask;
+1 -1
View File
@@ -69,7 +69,7 @@ namespace StarsiegeBot
if (args.Length > 0)
BotName = args[0].ToLower();
else
BotName = "raven";
BotName = "sspdev";
BotEventId = new EventId(276, BotName);
Events = new Triggers(BotEventId);
+2 -3
View File
@@ -8,8 +8,7 @@ namespace StarsiegeBot
{
class SnappleFacts : BaseCommandModule
{
private string[] facts;
private Random rnd = new Random();
private readonly string[] facts;
[Command("snapple"), Description("Get a Snapple \"Real Fact\".")]
public async Task Snapplefact(CommandContext ctx, [Description("[Optional] A Snapple Fact ID")]int snapId = -1)
@@ -17,7 +16,7 @@ namespace StarsiegeBot
if (snapId == -1)
{
await ctx.TriggerTypingAsync();
await ctx.RespondAsync(facts[rnd.Next(0, facts.Length)]);
await ctx.RespondAsync(facts[Program.rnd.Next(0, facts.Length)]);
}
else
{
+4 -5
View File
@@ -25,7 +25,6 @@ namespace StarsiegeBot
[Cooldown(1, 2, CooldownBucketType.Guild)]
class DeathMessages : BaseCommandModule
{
private readonly Random rnd = new Random();
private DeathMessageLines dmLines;
private bool DeathMessagesEnabled;
@@ -136,7 +135,7 @@ namespace StarsiegeBot
// if there is no target, we're going to "kill" the author.
if (target == null)
{
int choice = rnd.Next(0, dmLines.Generic.Length);
int choice = Program.rnd.Next(0, dmLines.Generic.Length);
line = dmLines.Generic[choice];
await ctx.RespondAsync(string.Format(line, ctx.Message.Author.Mention));
}
@@ -144,18 +143,18 @@ namespace StarsiegeBot
else
{
// Pick a death message type at random.
int opt = rnd.Next(1, 3);
int opt = Program.rnd.Next(1, 3);
// If we picked active death messages...
if (opt == 1)
{
// Pick an Active death message at random, and set our output to it.
int choice = rnd.Next(0, dmLines.Active.Length);
int choice = Program.rnd.Next(0, dmLines.Active.Length);
line = dmLines.Active[choice];
}
else if (opt == 2)
{
// We picked a Passive death message type. Pick the exact line at random. Set it to the output.
int choice = rnd.Next(0, dmLines.Passive.Length);
int choice = Program.rnd.Next(0, dmLines.Passive.Length);
line = dmLines.Passive[choice];
}
// Give the selected death message to the user.
+1 -2
View File
@@ -22,7 +22,6 @@ namespace StarsiegeBot
[Group("functions"), Aliases("func")]
class Functions : BaseCommandModule
{
private readonly Random rnd = new Random();
private Dictionary<string, SSFunction> ssFunctions;
private bool FunctionsEnabled;
@@ -94,7 +93,7 @@ namespace StarsiegeBot
// set the timestamp to now... why, idk, its not really needed actually.
embed.WithTimestamp(DateTime.Now);
// add a random color to the left side bar of the embed.
embed.WithColor(Program.colours[rnd.Next(0, Program.colours.Length)]);
embed.WithColor(Program.colours[Program.rnd.Next(0, Program.colours.Length)]);
// Message Description, I dont remember what it is any more, sorry!
embed.WithDescription(outItem.description);
// iterate over each field item, and add them to the embed.
+13 -7
View File
@@ -65,9 +65,11 @@ namespace StarsiegeBot
{
return;
}
DiscordEmbedBuilder embed = new DiscordEmbedBuilder();
embed.Title = "Master|Game Server Overview";
embed.Description = "Number of Master, and Game servers. Along with number of errors.";
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Title = "Master|Game Server Overview",
Description = "Number of Master, and Game servers. Along with number of errors."
};
embed.AddField("Masters", gameData.Masters.Length.ToString(),true);
embed.AddField("Games", gameData.Games.Length.ToString(),true);
embed.AddField("Errors", gameData.Errors.Length.ToString(),true);
@@ -91,8 +93,10 @@ namespace StarsiegeBot
{
return;
}
DiscordMessageBuilder msg = new DiscordMessageBuilder();
msg.Content = "Server Listing Errors:\r\n";
DiscordMessageBuilder msg = new DiscordMessageBuilder
{
Content = "Server Listing Errors:\r\n"
};
foreach (var item in gameData.Errors)
{
msg.Content += $"{item}\r\n";
@@ -117,8 +121,10 @@ namespace StarsiegeBot
{
return;
}
DiscordMessageBuilder msg = new DiscordMessageBuilder();
msg.Content = "```csharp\r\n";
DiscordMessageBuilder msg = new DiscordMessageBuilder
{
Content = "```csharp\r\n"
};
int count = 0;
foreach (var item in gameData.Masters)
{
+1 -2
View File
@@ -23,7 +23,6 @@ namespace StarsiegeBot
class Quickchat : BaseCommandModule
{
private List<Quickchats> Quickchats;
private readonly Random rnd = new Random();
private bool QuickChatsEnabled;
public Quickchat()
@@ -154,7 +153,7 @@ namespace StarsiegeBot
id = Math.Abs(id);
if (id == -1 || id > Quickchats.Count)
{
int idnum = rnd.Next(Quickchats.Count);
int idnum = Program.rnd.Next(Quickchats.Count);
chat = Quickchats[idnum];
msg.Content = $"[{idnum}] " + chat.text;
}
+34 -5
View File
@@ -27,6 +27,40 @@ namespace StarsiegeBot
[RequireGuild, RequirePermissions(Permissions.ManageChannels)]
class WelcomeMessage : BotSettings
{
[GroupCommand]
public async Task GeneralWelcome (CommandContext ctx)
{
await ctx.TriggerTypingAsync();
string gId = ctx.Guild.Id.ToString();
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
{
Description = "Welcome Message Info",
Color = Program.colours[0]
};
if (GuildSettings[gId].WelcomeChannel is null)
embed.AddField("Channel", "*None*");
else
embed.AddField("Channel", GuildSettings[gId].WelcomeChannel.Mention, true);
embed.AddField("Enabled?", GuildSettings[gId].UseWelcome.ToString(), true);
if (GuildSettings[gId].WelcomeMessage is null || GuildSettings[gId].WelcomeMessage == "")
embed.AddField("Message", "*None*");
else
embed.AddField("Message", GuildSettings[gId].WelcomeMessage);
await ctx.RespondAsync(embed);
}
[GroupCommand]
public async Task GeneralWelcome(CommandContext ctx, bool enable, DiscordChannel channel)
{
await WelcomeChannel(ctx, channel);
await WelcomeEnabled(ctx, enable);
}
[GroupCommand]
public async Task GeneralWelcome(CommandContext ctx, DiscordChannel channel, bool enable)
{
await WelcomeChannel(ctx, channel);
await WelcomeEnabled(ctx, enable);
}
[Command("enable")]
public async Task WelcomeEnabled(CommandContext ctx, bool enable)
{
@@ -43,7 +77,6 @@ namespace StarsiegeBot
embed.AddField("New", GuildSettings[gId].UseWelcome.ToString(), true);
await ctx.RespondAsync(embed);
}
[Command("Channel")]
public async Task WelcomeChannel(CommandContext ctx, DiscordChannel channel)
{
@@ -67,7 +100,6 @@ namespace StarsiegeBot
embed.AddField("New", GuildSettings[gId].WelcomeChannel.Mention, true);
await ctx.RespondAsync(embed);
}
[Command("Channel")]
public async Task WelcomeChannel(CommandContext ctx, [RemainingText]string here)
{
@@ -91,8 +123,6 @@ namespace StarsiegeBot
embed.AddField("New", GuildSettings[gId].WelcomeChannel.Mention, true);
await ctx.RespondAsync(embed);
}
[Command("Message")]
[Description("Hello. Run `>welcome help` for more info.")]
public async Task MessageOfWelcome(CommandContext ctx, [RemainingText]string msg)
@@ -120,7 +150,6 @@ namespace StarsiegeBot
Console.WriteLine("7");
await ctx.RespondAsync($"An example:\r\n> {example}", embed);
}
[Command("Help")]
public async Task WelcomeHelp(CommandContext ctx)
{