Archived
0
0
Fork 0

feat: switched to slash commands

This commit is contained in:
Daryl Ronningen 2021-10-09 19:07:39 -07:00
parent 2ae87f1c2d
commit 8b506d01b6
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
7 changed files with 132 additions and 141 deletions

View file

@ -5,19 +5,17 @@ using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using ByteSizeLib; using ByteSizeLib;
using DSharpPlus.CommandsNext; using DSharpPlus;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities; using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using Hardware.Info; using Hardware.Info;
namespace Sharpy.Commands.Msg.General namespace Sharpy.Commands.General
{ {
public class Info : BaseCommandModule public class Info : ApplicationCommandModule
{ {
[Command("info")] [SlashCommand("info", "Get info about the bot")]
[Cooldown(5, 5, CooldownBucketType.User)] public async Task InfoCommand(InteractionContext ctx)
[Description("Get info about the bot")]
public async Task InfoCommand(CommandContext ctx)
{ {
var clientMember = from member in ctx.Guild.Members var clientMember = from member in ctx.Guild.Members
where member.Value.Id == ctx.Client.CurrentUser.Id where member.Value.Id == ctx.Client.CurrentUser.Id
@ -25,7 +23,8 @@ namespace Sharpy.Commands.Msg.General
var roleColor = clientMember.First().Color; var roleColor = clientMember.First().Color;
var loadingMsg = await ctx.RespondAsync("Loading..."); await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder { Content = "Loading..." });
var hardwareInfo = new HardwareInfo(); var hardwareInfo = new HardwareInfo();
hardwareInfo.RefreshCPUList(); hardwareInfo.RefreshCPUList();
@ -57,8 +56,8 @@ namespace Sharpy.Commands.Msg.General
**Total RAM Usage**: {currentRamUsage}GB/{totalRamUsage}GB ({Math.Round(currentRamUsage / totalRamUsage * 100, 2)}%)") **Total RAM Usage**: {currentRamUsage}GB/{totalRamUsage}GB ({Math.Round(currentRamUsage / totalRamUsage * 100, 2)}%)")
.Build(); .Build();
await loadingMsg.DeleteAsync(); await ctx.CreateResponseAsync(InteractionResponseType.UpdateMessage,
await ctx.RespondAsync(embed); new DiscordInteractionResponseBuilder().AddEmbed(embed));
} }
} }
} }

View file

@ -1,47 +0,0 @@
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Lavalink;
namespace Sharpy.Commands.Msg.Music
{
public class Play : BaseCommandModule
{
[Command("play")]
[Cooldown(5, 5, CooldownBucketType.User)]
[Description("Plays a song")]
[RequireGuild]
public async Task PlayCommand(CommandContext ctx, [RemainingText] string search)
{
if (ctx.Member.VoiceState == null || ctx.Member.VoiceState.Channel == null)
{
await ctx.RespondAsync("You are not in a voice channel.");
return;
}
var lava = ctx.Client.GetLavalink();
if (!lava.ConnectedNodes.Any())
{
await ctx.RespondAsync("The Lavalink connection is not established");
return;
}
var node = lava.ConnectedNodes.Values.First();
var conn = await node.ConnectAsync(ctx.Member.VoiceState.Channel);
var loadResult = await node.Rest.GetTracksAsync(search);
if (loadResult.LoadResultType is LavalinkLoadResultType.LoadFailed or LavalinkLoadResultType.NoMatches)
{
await ctx.RespondAsync($"Track search failed for {search}.");
return;
}
var track = loadResult.Tracks.First();
await conn.PlayAsync(track);
await ctx.RespondAsync($"Now playing {track.Title}!");
}
}
}

View file

@ -1,48 +0,0 @@
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Lavalink;
namespace Sharpy.Commands.Msg.Music
{
public class Search : BaseCommandModule
{
[Command("search")]
[Cooldown(5, 5, CooldownBucketType.User)]
[Description("Searches for songs")]
[RequireGuild]
public async Task SearchCommand(CommandContext ctx, [RemainingText] string search)
{
var lava = ctx.Client.GetLavalink();
if (!lava.ConnectedNodes.Any())
{
await ctx.RespondAsync("The Lavalink connection is not established");
return;
}
var node = lava.ConnectedNodes.Values.First();
var loadResult = await node.Rest.GetTracksAsync(search);
if (loadResult.LoadResultType is LavalinkLoadResultType.LoadFailed or LavalinkLoadResultType.NoMatches)
{
await ctx.RespondAsync($"Track search failed for {search}.");
return;
}
var embed = new DiscordEmbedBuilder();
embed.WithTitle($"Search Results for: `{search}`");
foreach (var loadResultTrack in loadResult.Tracks)
{
embed.AddField($"**{loadResultTrack.Title}**", @$"**URL**: {loadResultTrack.Uri}
**Author**: {loadResultTrack.Author}
**Length**: {loadResultTrack.Length}", true);
}
await ctx.RespondAsync(embed.Build());
}
}
}

View file

@ -0,0 +1,51 @@
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Lavalink;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
namespace Sharpy.Commands.Music
{
public class Play : ApplicationCommandModule
{
[SlashCommand("play", "Plays a song")]
[SlashRequireGuild]
public async Task PlayCommand(InteractionContext ctx, [Option("song", "The song to play. (Accepts YouTube URLs too)")] string song)
{
if (ctx.Member.VoiceState == null || ctx.Member.VoiceState.Channel == null)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder { Content = "You are not in a voice channel." });
return;
}
var lava = ctx.Client.GetLavalink();
if (!lava.ConnectedNodes.Any())
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder { Content = "The Lavalink connection is not established" });
return;
}
var node = lava.ConnectedNodes.Values.First();
var conn = await node.ConnectAsync(ctx.Member.VoiceState.Channel);
var loadResult = await node.Rest.GetTracksAsync(song);
if (loadResult.LoadResultType is LavalinkLoadResultType.LoadFailed or LavalinkLoadResultType.NoMatches)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder { Content = $"Track search failed for {song}." });
return;
}
var track = loadResult.Tracks.First();
await conn.PlayAsync(track);
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder { Content = $"Now playing {track.Title}!" });
}
}
}

View file

@ -0,0 +1,48 @@
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Lavalink;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
namespace Sharpy.Commands.Music
{
public class Search : ApplicationCommandModule
{
[SlashCommand("search", "Searches for songs")]
[SlashRequireGuild]
public async Task SearchCommand(InteractionContext ctx, [Option("search", "The song to search for. (Accepts YouTube URLs too)")] string search)
{
var lava = ctx.Client.GetLavalink();
if (!lava.ConnectedNodes.Any())
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder { Content = "The Lavalink connection is not established" });
return;
}
var node = lava.ConnectedNodes.Values.First();
var loadResult = await node.Rest.GetTracksAsync(search);
if (loadResult.LoadResultType is LavalinkLoadResultType.LoadFailed or LavalinkLoadResultType.NoMatches)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder { Content = $"Track search failed for {search}." });
return;
}
var embed = new DiscordEmbedBuilder();
embed.WithTitle($"Search Results for: `{search}`");
foreach (var loadResultTrack in loadResult.Tracks)
embed.AddField($"**{loadResultTrack.Title}**", @$"**URL**: {loadResultTrack.Uri}
**Author**: {loadResultTrack.Author}
**Length**: {loadResultTrack.Length}", true);
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().AddEmbed(embed.Build()));
}
}
}

View file

@ -1,14 +1,15 @@
using System; using System;
using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using DSharpPlus; using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities; using DSharpPlus.Entities;
using DSharpPlus.Lavalink; using DSharpPlus.Lavalink;
using DSharpPlus.Net; using DSharpPlus.Net;
using DSharpPlus.SlashCommands;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Sharpy.Commands.General;
using Sharpy.Commands.Music;
IConfiguration config = new ConfigurationBuilder() IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true) .AddJsonFile("appsettings.json", false, true)
@ -47,34 +48,22 @@ var discord = new DiscordClient(new DiscordConfiguration
var services = new ServiceCollection() var services = new ServiceCollection()
.BuildServiceProvider(); .BuildServiceProvider();
/*
var interactivity = discord.UseInteractivity();
*/
// Register Msg Commands
var commands = discord.UseCommandsNext(new CommandsNextConfiguration
{
CaseSensitive = false,
DmHelp = false,
EnableDefaultHelp = true,
EnableDms = true,
EnableMentionPrefix = true,
IgnoreExtraArguments = false,
Services = services,
StringPrefixes = new[] { "!" },
UseDefaultCommandHandler = true
});
commands.RegisterCommands(Assembly.GetExecutingAssembly());
// Register Lavalink // Register Lavalink
var lavalink = discord.UseLavalink(); var lavalink = discord.UseLavalink();
/*// Register Slash Commands // Register Slash Commands
var slash = discord.UseSlashCommands(new SlashCommandsConfiguration var slash = discord.UseSlashCommands(new SlashCommandsConfiguration
{ {
Services = services Services = services
});*/ });
// Register Commands
// General
slash.RegisterCommands<Info>(772228301552222258);
// Music
slash.RegisterCommands<Play>(772228301552222258);
slash.RegisterCommands<Search>(772228301552222258);
await discord.ConnectAsync(new DiscordActivity("With DSharpPlus")); await discord.ConnectAsync(new DiscordActivity("With DSharpPlus"));
await lavalink.ConnectAsync(new LavalinkConfiguration await lavalink.ConnectAsync(new LavalinkConfiguration

View file

@ -34,12 +34,11 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="ByteSize" Version="2.1.0"/> <PackageReference Include="ByteSize" Version="2.1.0"/>
<PackageReference Include="DSharpPlus" Version="4.2.0-nightly-01011" /> <PackageReference Include="DSharpPlus" Version="4.2.0-nightly-01024"/>
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.2.0-nightly-01011" /> <PackageReference Include="DSharpPlus.Interactivity" Version="4.2.0-nightly-01024"/>
<PackageReference Include="DSharpPlus.Interactivity" Version="4.2.0-nightly-01011" /> <PackageReference Include="DSharpPlus.Lavalink" Version="4.2.0-nightly-01024"/>
<PackageReference Include="DSharpPlus.Lavalink" Version="4.2.0-nightly-01011" /> <PackageReference Include="DSharpPlus.SlashCommands" Version="4.2.0-nightly-01024"/>
<PackageReference Include="Hardware.Info" Version="1.1.1.1"/> <PackageReference Include="Hardware.Info" Version="1.1.1.1"/>
<PackageReference Include="IDoEverything.DSharpPlus.SlashCommands" Version="2.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0-rc.1.21451.13"/> <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0-rc.1.21451.13"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0-rc.1.21451.13"/> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0-rc.1.21451.13"/>
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0-rc.1.21451.13"/> <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0-rc.1.21451.13"/>