Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using DSharpPlus.Entities;
- using DSharpPlus.SlashCommands;
- using DSharpPlus;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using DSharpPlus.VoiceNext;
- using System.Diagnostics;
- using System.IO;
- using SharpCompress.Common;
- namespace BotName.Bot.Commands.Fun.SlashCommands
- {
- //The Command class.
- public class SoundCommands : ApplicationCommandModule
- {
- //The command.
- [SlashCommand("play", "plays a sound in a voice channel.")]
- public async Task HowlCommand(InteractionContext ctx, [Choice("ChoiceName", "C:\\My\\Program\\Directory\\Name\\MySound.mp3")]
- [Option("Sound", "Please select a Sound")] string filepath)
- {
- //Creates a slash command used response.
- //Also removes the error message.
- await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
- .WithContent("Playing sound in voice channel. Please wait just a moment!"));
- //Checks if the user is not a bot to send the message.
- if (ctx.Member.IsBot)
- {
- return;
- }
- else
- {
- if(filepath != "C:\\My\\Program\\Directory\\Name\\MySound.mp3")
- {
- var embedmessage = new DiscordMessageBuilder()
- .AddEmbed(new DiscordEmbedBuilder()
- .WithAuthor("BotName", null, ctx.Client.CurrentApplication.Icon)
- .WithTitle("Please select the following sound to play:")
- .WithImageUrl(ctx.Client.CurrentApplication.Icon)
- .WithFooter("VoiceChannel Error.", "ImageURL.png")
- .WithTimestamp(DateTime.Now)
- .Build()
- );
- //Makes the command wait 5 seconds before sending the rest of the command data.
- await Task.Delay(TimeSpan.FromSeconds(5));
- //Sends the embed in a message.
- await ctx.Channel.SendMessageAsync(embedmessage);
- }
- else
- {
- //Makes the command wait 5 seconds before sending the rest of the command data.
- await Task.Delay(TimeSpan.FromSeconds(5));
- var vnext = ctx.Client.GetVoiceNext();
- var vnc = vnext.GetConnection(ctx.Guild);
- //if null throws exception.
- if (vnc == null)
- throw new System.InvalidOperationException("Not connected in this guild.");
- //Gets the mp3 file to use.
- var ffmpeg = Process.Start(new ProcessStartInfo
- {
- FileName = "ffmpeg",
- Arguments = $@"-i ""{filepath}"" -ac 2 -f s16le -ar 48000 pipe:1",
- RedirectStandardOutput = true,
- UseShellExecute = false
- });
- Stream pcm = ffmpeg.StandardOutput.BaseStream;
- VoiceTransmitSink transmit = vnc.GetTransmitSink();
- await pcm.CopyToAsync(transmit);
- vnc.GetTransmitSink().VolumeModifier = 5;
- //Makes the command wait 10 seconds before sending the rest of the command data.
- await Task.Delay(TimeSpan.FromSeconds(10));
- //Disconnects the bot from the voice channel.
- vnc.Disconnect();
- }
- }
- }
- //The command.
- [SlashCommand("join", "Joins a voice channel.")]
- public async Task JoinChannel(InteractionContext ctx, [Choice("MyVoiceChannel", "VoiceChannelName")]
- [Option("VoiceChannel", "Please choose a Voice Channel.")] DiscordChannel channel)
- {
- //Creates a slash command used response.
- //Also removes the error message.
- await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
- .WithContent("Joining voice channel. Please wait just a moment!"));
- //Checks if the user is not a bot to send the message.
- if (ctx.Member.IsBot)
- {
- return;
- }
- else
- {
- if (channel.Name != "MyVoiceChannelName")
- {
- var embedmessage = new DiscordMessageBuilder()
- .AddEmbed(new DiscordEmbedBuilder()
- .WithAuthor("BotName", null, ctx.Client.CurrentApplication.Icon)
- .WithTitle("Please Create The Following Voice Channel:")
- .WithImageUrl(ctx.Client.CurrentApplication.Icon)
- .AddField("VoiceChannel:", "**BotName**" + Environment.NewLine + "Is Case Sensitive: **Yes**")
- .WithFooter("VoiceChannel Error.", "ImageURL.png")
- .WithTimestamp(DateTime.Now)
- .Build()
- );
- //Makes the command wait 5 seconds before sending the rest of the command data.
- await Task.Delay(TimeSpan.FromSeconds(5));
- //Sends the embed in a message.
- await ctx.Channel.SendMessageAsync(embedmessage);
- }
- else
- {
- //Makes the command wait 5 seconds before sending the rest of the command data.
- await Task.Delay(TimeSpan.FromSeconds(5));
- channel = ctx.Member.VoiceState?.Channel;
- await channel.ConnectAsync();
- }
- }
- }
- }
- }
- # My Program Class Code is as follows:
- using BotName.Bot.Util;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using DSharpPlus;
- using DSharpPlus.CommandsNext;
- using DSharpPlus.AsyncEvents;
- using DSharpPlus.Interactivity;
- using DSharpPlus.EventArgs;
- using DSharpPlus.Interactivity.Extensions;
- using BotName.Bot.Commands.Fun.SlashCommands;
- using DSharpPlus.CommandsNext.Exceptions;
- using DSharpPlus.CommandsNext.Attributes;
- using DSharpPlus.Entities;
- using System.Reflection.Emit;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.DependencyInjection;
- using DSharpPlus.SlashCommands;
- using DSharpPlus.VoiceNext;`
- namespace BotName
- {
- public sealed class Program
- {
- public static DiscordClient Client { get; private set; }
- public static InteractivityExtension Interactivity { get; private set; }
- public static CommandsNextExtension Commands { get; private set; }
- public static VoiceNextExtension VoiceNext { get; private set; }
- static async Task Main(string[] args)
- {
- //Main Window configs specifying the title name and color.
- Console.BackgroundColor = ConsoleColor.Black;
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.Title = "BotName";
- //1. Get the details of your config.json file by deserialising it
- var configJsonFile = new JSONReader();
- await configJsonFile.ReadJSON();
- //2. Setting up the Bot Configuration
- var discordConfig = new DiscordConfiguration()
- {
- Intents = DiscordIntents.All,
- Token = configJsonFile.token,
- TokenType = TokenType.Bot,
- AutoReconnect = true
- };
- //3. Apply this config to our DiscordClient
- Client = new DiscordClient(discordConfig);
- //4. Set the default timeout for Commands that use interactivity
- Client.UseInteractivity(new InteractivityConfiguration()
- {
- Timeout = TimeSpan.FromMinutes(2)
- });
- //5. Set up the Task Handler Ready event
- Client.Ready += OnClientReady;
- //6. Set up the Commands Configuration
- var commandsConfig = new CommandsNextConfiguration()
- {
- StringPrefixes = new string[] { configJsonFile.prefix },
- EnableMentionPrefix = true,
- EnableDms = true,
- EnableDefaultHelp = false,
- };
- Commands = Client.UseCommandsNext(commandsConfig);
- //7. Register your commands
- var slashCommandsConfig = Client.UseSlashCommands();
- slashCommandsConfig.RegisterCommands<MySoundsCommand>(MyGuildID);
- //8. Allows usage of voice channels.
- var VoiceNext = Client.UseVoiceNext();
- //9. Connect to get the Bot online
- await Client.ConnectAsync();
- await Task.Delay(-1);
- }
- private static Task OnClientReady(DiscordClient sender, ReadyEventArgs e)
- {
- return Task.CompletedTask;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement