This commit is contained in:
2022-08-16 16:14:25 +03:00
parent 2fcd86cf12
commit 1f1983480a
32 changed files with 195 additions and 65 deletions

13
.idea/.idea.SethDiscordBot/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/projectSettingsUpdater.xml
/contentModel.xml
/.idea.SethDiscordBot.iml
/modules.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

1
.idea/.idea.SethDiscordBot/.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
SethDiscordBot

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -11,6 +11,8 @@ internal class Level : DBCommand
{
public string Command => "level";
public List<string> Aliases => new() { "lvl" };
public string Description => "Display tour current level";
public string Usage => "level";

View File

@@ -1,11 +1,14 @@
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces;
using System.Collections.Generic;
internal class Echo : DBCommand
{
public string Command => "echo";
public List<string> Aliases => null;
public string Description => "Replay with the same message";
public string Usage => "echo [message]";

View File

@@ -1,6 +1,7 @@
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces;
using System.Collections.Generic;
namespace CMD_Utils;
@@ -8,6 +9,8 @@ internal class FlipCoin : DBCommand
{
public string Command => "flip";
public List<string> Aliases => null;
public string Description => "Flip a coin";
public string Usage => "flip";

View File

@@ -11,6 +11,8 @@ public class Poll : DBCommand
{
public string Command => "poll";
public List<string> Aliases => null;
public string Description => "Create a poll with options";
public string Usage => "poll [This-is-question] [This-is-answer-1] [This-is-answer-2] ... ";

View File

@@ -1,4 +1,5 @@
using Discord.Commands;
using System.Collections.Generic;
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces;
@@ -6,6 +7,8 @@ public class Random : DBCommand
{
public string Command => "random";
public List<string> Aliases => new() { "rnd" };
public string Description => "random number between number1 and number2";
public string Usage => "random [number1] [number2]";

View File

@@ -1,4 +1,6 @@
using Discord;
using System.Collections.Generic;
using System.Linq;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces;
@@ -17,15 +19,17 @@ internal class Help : DBCommand
/// </summary>
public string Command => "help";
public List<string> Aliases => null;
/// <summary>
/// Command Description
/// </summary>
public string Description => "This command allows you to check all loadded commands";
public string Description => "This command allows you to check all loaded commands";
/// <summary>
/// Command usage
/// </summary>
public string Usage => "help";
public string Usage => "help <command>";
/// <summary>
/// Check if the command can be used <inheritdoca DM <see cref="IChannel" />/>
@@ -57,10 +61,10 @@ internal class Help : DBCommand
foreach (var item in args)
{
var e = GenerateHelpCommand(item);
if (e != null)
context.Channel.SendMessageAsync(embed: e.Build());
else
if (e is null)
context.Channel.SendMessageAsync("Unknown Command " + item);
else
context.Channel.SendMessageAsync(embed: e.Build());
}
return;
@@ -74,10 +78,12 @@ internal class Help : DBCommand
foreach (var cmd in PluginLoader.Commands!)
{
if (cmd.canUseDM) DMCommands += cmd.Command + " ";
if (cmd.canUseDM)
DMCommands += cmd.Command + " ";
if (cmd.requireAdmin)
adminCommands += cmd.Command + " ";
else if (cmd.canUseServer) normalCommands += cmd.Command + " ";
if (cmd.canUseServer)
normalCommands += cmd.Command + " ";
}
embedBuilder.AddField("Admin Commands", adminCommands);
@@ -89,11 +95,14 @@ internal class Help : DBCommand
private EmbedBuilder GenerateHelpCommand(string command)
{
var embedBuilder = new EmbedBuilder();
var cmd = PluginLoader.Commands.Find(p => p.Command == command);
var cmd = PluginLoader.Commands!.Find(p => p.Command == command || (p.Aliases is not null && p.Aliases.Contains(command)));
if (cmd == null) return null;
embedBuilder.AddField("Usage", cmd.Usage);
embedBuilder.AddField("Description", cmd.Description);
if (cmd.Aliases is null)
return embedBuilder;
embedBuilder.AddField("Alias", cmd.Aliases.Count == 0 ? "-" : string.Join(", ", cmd.Aliases));
return embedBuilder;
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Discord.WebSocket;
using PluginManager.Interfaces;
@@ -17,6 +18,8 @@ internal class Restart : DBCommand
/// </summary>
public string Command => "restart";
public List<string> Aliases => null;
/// <summary>
/// Command Description
/// </summary>

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
@@ -14,6 +15,8 @@ internal class Settings : DBCommand
/// </summary>
public string Command => "set";
public List<string> Aliases => null;
/// <summary>
/// Command Description
/// </summary>

View File

@@ -60,7 +60,9 @@ internal class Boot
/// <returns>Task</returns>
public async Task Awake()
{
client = new DiscordSocketClient();
DiscordSocketConfig config = new DiscordSocketConfig { AlwaysDownloadUsers = true };
client = new DiscordSocketClient(config);
service = new CommandService();
CommonTasks();

View File

@@ -47,13 +47,16 @@ internal class CommandHandler
{
try
{
if (Message as SocketUserMessage == null) return;
if (Message as SocketUserMessage == null)
return;
var message = Message as SocketUserMessage;
if (message == null) return;
if (message == null)
return;
if (!message.Content.StartsWith(botPrefix)) return;
if (!message.Content.StartsWith(botPrefix))
return;
var argPos = 0;
@@ -63,17 +66,14 @@ internal class CommandHandler
return;
}
if (message.Author.IsBot) return;
if (message.Author.IsBot)
return;
var context = new SocketCommandContext(client, message);
await commandService.ExecuteAsync(
context,
argPos,
null
);
await commandService.ExecuteAsync(context, argPos, null);
var plugin = PluginLoader.Commands!.Where(p => p.Command == message.Content.Split(' ')[0].Substring(botPrefix.Length)).FirstOrDefault();
var plugin = PluginLoader.Commands!.Where(p => p.Command == message.Content.Split(' ')[0].Substring(botPrefix.Length) || (p.Aliases is not null && p.Aliases.Contains(message.Content.Split(' ')[0].Substring(botPrefix.Length)))).FirstOrDefault();
if (plugin != null)

View File

@@ -8,7 +8,7 @@
<StartupObject />
<SignAssembly>False</SignAssembly>
<IsPublishable>True</IsPublishable>
<AssemblyVersion>1.0.0.1</AssemblyVersion>
<AssemblyVersion>1.0.0.2</AssemblyVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View File

@@ -34,6 +34,38 @@ public class Program
Directory.CreateDirectory("./Data/Plugins/Events");
PreLoadComponents().Wait();
if (!Config.ContainsKey("ServerID"))
{
do
{
Console.Clear();
Console.WriteLine("Please enter the server ID: ");
Console_Utilities.WriteColorText("You can find it in the Server Settings at &r\"Widget\"&c section");
Console.WriteLine("Example: 1234567890123456789");
Console.WriteLine("This is not required, but is recommended. If you refuse to provide the ID, just press enter.\nThe server id is required to make easier for the bot to interact with the server.\nRemember: this bot is for one server ONLY.");
Console.Write("User Input > ");
ConsoleKeyInfo key = Console.ReadKey();
if (key.Key == ConsoleKey.Enter)
Config.AddValueToVariables("ServerID", "null", false);
else
{
string SID = key.KeyChar + Console.ReadLine();
if (SID.Length != 18)
{
Console.WriteLine("Your server ID is not 18 characters long. Please try again.");
continue;
}
Config.AddValueToVariables("ServerID", SID, false);
}
break;
} while (true);
}
if (!Config.ContainsKey("token") || Config.GetValue<string>("token") == null || Config.GetValue<string>("token")?.Length != 70)
{
Console.WriteLine("Please insert your token");
@@ -75,9 +107,13 @@ public class Program
private static void NoGUI(Boot discordbooter)
{
var consoleCommandsHandler = new ConsoleCommandsHandler(discordbooter.client);
#if DEBUG
Console.WriteLine();
consoleCommandsHandler.HandleCommand("lp");
#else
if (loadPluginsOnStartup) consoleCommandsHandler.HandleCommand("lp");
if (listPluginsAtStartup) consoleCommandsHandler.HandleCommand("listplugs");
#endif
Config.SaveConfig();
while (true)
@@ -264,9 +300,9 @@ public class Program
Config.PluginConfig.Load();
if (!Config.ContainsKey("Version"))
Config.AddValueToVariables("Version", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), false);
Config.AddValueToVariables("Version", Assembly.GetExecutingAssembly().GetName().Version.ToString(), false);
else
Config.SetValue("Version", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
Config.SetValue("Version", Assembly.GetExecutingAssembly().GetName().Version.ToString());
foreach (var key in OnlineDefaultKeys)
{
@@ -311,7 +347,7 @@ public class Program
Console.WriteLine("\n\n");
await Task.Delay(1000);
int waitTime = 20; //wait time to proceed
int waitTime = 10; //wait time to proceed
Console.Write($"The bot will start in {waitTime} seconds");
while (waitTime > 0)

View File

@@ -1,4 +1,5 @@
using Discord.Commands;
using System.Collections.Generic;
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces;
@@ -8,6 +9,8 @@ internal class Leave : DBCommand
{
public string Command => "leave";
public List<string> Aliases => null;
public string Description => "Leave the voice channel";
public string Usage => "leave";

View File

@@ -6,6 +6,7 @@ namespace MusicCommands;
internal class MusicPlayer
{
private Stream outputStream { get; }
internal AudioFile NowPlaying = null;
internal bool isPlaying, isPaused;
@@ -14,9 +15,10 @@ internal class MusicPlayer
outputStream = outputChannel;
}
public async Task Play(Stream source, int byteSize)
public async Task Play(Stream source, int byteSize, AudioFile songPlaying)
{
isPlaying = true;
NowPlaying = songPlaying;
while (isPlaying)
{
if (isPaused)

View File

@@ -1,4 +1,5 @@
using Discord.Commands;
using System.Collections.Generic;
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces;
@@ -8,6 +9,8 @@ internal class Pause : DBCommand
{
public string Command => "pause";
public List<string> Aliases => null;
public string Description => "Pause/Unpause the music that is currently running";
public string Usage => "pause";

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Discord;
@@ -14,6 +15,8 @@ internal class Play : DBCommand
{
public string Command => "play";
public List<string> Aliases => new() { "p" };
public string Description => "Play music from a file";
public string Usage => "play [name/url]";
@@ -69,11 +72,11 @@ internal class Play : DBCommand
if (Data.MusicPlayer is not null)
{
await context.Channel.SendMessageAsync("Enqueued your request");
await context.Channel.SendMessageAsync("Queued your request: " + splitted.MergeStrings(1));
return;
}
}
while (false);
while (false); // run only one time !
Data.voiceChannel = (context.User as IGuildUser)?.VoiceChannel;
@@ -93,15 +96,14 @@ internal class Play : DBCommand
using (var discordChanneAudioOutStream = Data.audioClient.CreatePCMStream(AudioApplication.Mixed))
{
if (Data.MusicPlayer is null)
Data.MusicPlayer = new MusicPlayer(discordChanneAudioOutStream);
Data.MusicPlayer ??= new MusicPlayer(discordChanneAudioOutStream);
while (Data.Playlist.Count > 0)
{
var nowPlaying = Data.Playlist.GetNextSong;
using (var ffmpeg = CreateStream(nowPlaying.Name))
using (var ffmpegOutputBaseStream = ffmpeg.StandardOutput.BaseStream)
{
await Data.MusicPlayer.Play(ffmpegOutputBaseStream, 1024);
await Data.MusicPlayer.Play(ffmpegOutputBaseStream, 1024, nowPlaying);
Console.WriteLine("Finished playing from " + nowPlaying.Url);
}
}
@@ -110,7 +112,7 @@ internal class Play : DBCommand
}
}
private Process CreateStream(string path)
private static Process CreateStream(string path)
{
return Process.Start(new ProcessStartInfo { FileName = "ffmpeg", Arguments = $"-hide_banner -loglevel panic -i \"{path}\" -ac 2 -f s16le -ar 48000 pipe:1", UseShellExecute = false, RedirectStandardOutput = true });
}

View File

@@ -13,6 +13,8 @@ namespace MusicCommands
{
public string Command => "skip";
public List<string> Aliases => null;
public string Description => "skip the music that is currently running";
public string Usage => "skip";
@@ -25,7 +27,16 @@ namespace MusicCommands
public void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
{
var loadedSong = Data.MusicPlayer.NowPlaying;
if (loadedSong is null || Data.MusicPlayer.isPlaying == false)
{
message.Channel.SendMessageAsync("There is no music playing");
return;
}
Data.MusicPlayer.isPlaying = false;
message.Channel.SendMessageAsync($"You have skipped {loadedSong.Name}");
}
}
}

View File

@@ -12,6 +12,7 @@ namespace MusicCommands
public class queue : DBCommand
{
public string Command => "queue";
public List<string> Aliases => new() { "q" };
public string Description => "check queue";

View File

@@ -67,9 +67,9 @@ namespace PluginManager
public static PluginType GetPluginType(string pluginName)
{
foreach (var tuple in InstalledPlugins)
{
if (tuple.Item1 == pluginName) return tuple.Item2;
}
if (tuple.Item1 == pluginName)
return tuple.Item2;
return PluginType.Unknown;
}

View File

@@ -1,4 +1,5 @@
using Discord.Commands;
using System.Collections.Generic;
using Discord.Commands;
using Discord.WebSocket;
namespace PluginManager.Interfaces;
@@ -11,6 +12,11 @@ public interface DBCommand
/// </summary>
string Command { get; }
/// <summary>
/// Command aliases. Users may use this to execute the command
/// </summary>
List<string>? Aliases { get; }
/// <summary>
/// Command description
/// </summary>
@@ -44,8 +50,5 @@ public interface DBCommand
/// <param name="message">The message that the user types</param>
/// <param name="client">The discord client of the bot</param>
/// <param name="isDM">true if the message was sent from DM, otherwise false. It is always false if canUseDM is false</param>
void Execute(SocketCommandContext context,
SocketMessage message,
DiscordSocketClient client,
bool isDM);
void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM);
}

View File

@@ -44,8 +44,8 @@ public class Command
public class ConsoleCommand
{
public string CommandName { get; set; }
public string Description { get; set; }
public string Usage { get; set; }
public Action<string[]> Action { get; set; }
public string CommandName { get; init; }
public string Description { get; init; }
public string Usage { get; init; }
public Action<string[]> Action { get; init; }
}

View File

@@ -233,15 +233,15 @@ public class ConsoleCommandsHandler
}
);
AddCommand("sd", "Shuts down the discord bot", async () =>
AddCommand("sd", "Shuts down the discord bot", () =>
{
if (client is null)
return;
await client.StopAsync();
await client.DisposeAsync();
client.StopAsync();
client.DisposeAsync();
Config.SaveConfig();
Console.WriteLine("Bot is closing in 2 seconds ! Please wait to save data !");
await Task.Delay(2000);
Thread.Sleep(2000);
Environment.Exit(0);
}
);
@@ -268,7 +268,7 @@ public class ConsoleCommandsHandler
public static bool CommandExists(string command)
{
return !(GetCommand(command) is null);
return GetCommand(command) is not null;
}
public static ConsoleCommand? GetCommand(string command)

View File

@@ -175,7 +175,7 @@ namespace PluginManager.Others
int currentZIPFile = 0;
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith("/"))
if (entry.FullName.EndsWith("/")) // it is a folder
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
else

View File

@@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EVE_LevelingSystem", "EVE_L
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CMD_LevelingSystem", "CMD_LevelingSystem\CMD_LevelingSystem.csproj", "{1A4E49FF-9A0A-4C54-AF35-CFFBA64353D9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Roles", "Roles\Roles.csproj", "{954F2AA9-6624-4554-946D-0F17B84487C3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -51,6 +53,10 @@ Global
{1A4E49FF-9A0A-4C54-AF35-CFFBA64353D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A4E49FF-9A0A-4C54-AF35-CFFBA64353D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A4E49FF-9A0A-4C54-AF35-CFFBA64353D9}.Release|Any CPU.Build.0 = Release|Any CPU
{954F2AA9-6624-4554-946D-0F17B84487C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{954F2AA9-6624-4554-946D-0F17B84487C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{954F2AA9-6624-4554-946D-0F17B84487C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{954F2AA9-6624-4554-946D-0F17B84487C3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -62,6 +68,7 @@ Global
{B1B4976E-5112-4217-B57B-3A03C5207B6E} = {449FA364-0B72-43FF-B3A3-806E2916200E}
{EEC445DC-0C4B-43EA-8694-606BA0390B77} = {A290C028-77C4-4D1D-AB43-DDFE6ABD9012}
{1A4E49FF-9A0A-4C54-AF35-CFFBA64353D9} = {449FA364-0B72-43FF-B3A3-806E2916200E}
{954F2AA9-6624-4554-946D-0F17B84487C3} = {449FA364-0B72-43FF-B3A3-806E2916200E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3FB3C5DE-ED21-4D2E-ABDD-3A00EE4A2FFF}