Advertisement
IngeniousThoughts

My FFMPEG Command Source

May 19th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.50 KB | Source Code | 0 0
  1. using DSharpPlus.Entities;
  2. using DSharpPlus.SlashCommands;
  3. using DSharpPlus;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using DSharpPlus.VoiceNext;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using SharpCompress.Common;
  13.  
  14. namespace BotName.Bot.Commands.Fun.SlashCommands
  15. {
  16.     //The Command class.
  17.     public class SoundCommands : ApplicationCommandModule
  18.     {
  19.  
  20.         //The command.
  21.         [SlashCommand("play", "plays a sound in a voice channel.")]
  22.         public async Task HowlCommand(InteractionContext ctx, [Choice("ChoiceName", "C:\\My\\Program\\Directory\\Name\\MySound.mp3")]
  23.                                                              [Option("Sound", "Please select a Sound")] string filepath)
  24.         {
  25.             //Creates a slash command used response.
  26.             //Also removes the error message.
  27.             await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
  28.                                                                                                                     .WithContent("Playing sound in voice channel. Please wait just a moment!"));
  29.  
  30.             //Checks if the user is not a bot to send the message.
  31.             if (ctx.Member.IsBot)
  32.             {
  33.                 return;
  34.             }
  35.             else
  36.             {
  37.                 if(filepath != "C:\\My\\Program\\Directory\\Name\\MySound.mp3")
  38.                 {
  39.                     var embedmessage = new DiscordMessageBuilder()
  40.                         .AddEmbed(new DiscordEmbedBuilder()
  41.  
  42.                         .WithAuthor("BotName", null, ctx.Client.CurrentApplication.Icon)
  43.                         .WithTitle("Please select the following sound to play:")
  44.                         .WithImageUrl(ctx.Client.CurrentApplication.Icon)
  45.                         .WithFooter("VoiceChannel Error.", "ImageURL.png")
  46.                         .WithTimestamp(DateTime.Now)
  47.                         .Build()
  48.  
  49.                         );
  50.  
  51.                     //Makes the command wait 5 seconds before sending the rest of the command data.
  52.                     await Task.Delay(TimeSpan.FromSeconds(5));
  53.  
  54.                     //Sends the embed in a message.
  55.                     await ctx.Channel.SendMessageAsync(embedmessage);
  56.                 }
  57.                 else
  58.                 {
  59.                     //Makes the command wait 5 seconds before sending the rest of the command data.
  60.                     await Task.Delay(TimeSpan.FromSeconds(5));
  61.  
  62.  
  63.                     var vnext = ctx.Client.GetVoiceNext();
  64.                     var vnc = vnext.GetConnection(ctx.Guild);
  65.                    
  66.                     //if null throws exception.
  67.                     if (vnc == null)
  68.                         throw new System.InvalidOperationException("Not connected in this guild.");
  69.  
  70.  
  71.                     //Gets the mp3 file to use.
  72.                     var ffmpeg = Process.Start(new ProcessStartInfo
  73.                     {
  74.                         FileName = "ffmpeg",
  75.                         Arguments = $@"-i ""{filepath}"" -ac 2 -f s16le -ar 48000 pipe:1",
  76.                         RedirectStandardOutput = true,
  77.                         UseShellExecute = false
  78.                     });
  79.                     Stream pcm = ffmpeg.StandardOutput.BaseStream;
  80.  
  81.                     VoiceTransmitSink transmit = vnc.GetTransmitSink();
  82.                     await pcm.CopyToAsync(transmit);
  83.                     vnc.GetTransmitSink().VolumeModifier = 5;
  84.  
  85.                     //Makes the command wait 10 seconds before sending the rest of the command data.
  86.                     await Task.Delay(TimeSpan.FromSeconds(10));
  87.  
  88.                     //Disconnects the bot from the voice channel.
  89.                     vnc.Disconnect();
  90.                 }
  91.             }
  92.         }
  93.  
  94.         //The command.
  95.         [SlashCommand("join", "Joins a voice channel.")]
  96.         public async Task JoinChannel(InteractionContext ctx, [Choice("MyVoiceChannel", "VoiceChannelName")]
  97.                                                              [Option("VoiceChannel", "Please choose a Voice Channel.")] DiscordChannel channel)
  98.         {
  99.             //Creates a slash command used response.
  100.             //Also removes the error message.
  101.             await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
  102.                                                                                                                     .WithContent("Joining voice channel. Please wait just a moment!"));
  103.  
  104.             //Checks if the user is not a bot to send the message.
  105.             if (ctx.Member.IsBot)
  106.             {
  107.                 return;
  108.             }
  109.             else
  110.             {
  111.                 if (channel.Name != "MyVoiceChannelName")
  112.                 {
  113.                     var embedmessage = new DiscordMessageBuilder()
  114.                         .AddEmbed(new DiscordEmbedBuilder()
  115.  
  116.                         .WithAuthor("BotName", null, ctx.Client.CurrentApplication.Icon)
  117.                         .WithTitle("Please Create The Following Voice Channel:")
  118.                         .WithImageUrl(ctx.Client.CurrentApplication.Icon)
  119.                         .AddField("VoiceChannel:", "**BotName**" + Environment.NewLine + "Is Case Sensitive: **Yes**")
  120.                         .WithFooter("VoiceChannel Error.", "ImageURL.png")
  121.                         .WithTimestamp(DateTime.Now)
  122.                         .Build()
  123.  
  124.                         );
  125.  
  126.                     //Makes the command wait 5 seconds before sending the rest of the command data.
  127.                     await Task.Delay(TimeSpan.FromSeconds(5));
  128.  
  129.                     //Sends the embed in a message.
  130.                     await ctx.Channel.SendMessageAsync(embedmessage);
  131.                 }
  132.                 else
  133.                 {
  134.                     //Makes the command wait 5 seconds before sending the rest of the command data.
  135.                     await Task.Delay(TimeSpan.FromSeconds(5));
  136.  
  137.  
  138.                     channel = ctx.Member.VoiceState?.Channel;
  139.                     await channel.ConnectAsync();
  140.  
  141.                 }
  142.             }
  143.         }
  144.  
  145.     }
  146. }
  147.  
  148. # My Program Class Code is as follows:
  149.  
  150. using BotName.Bot.Util;
  151. using System;
  152. using System.Collections.Generic;
  153. using System.Linq;
  154. using System.Text;
  155. using System.Threading.Tasks;
  156. using Newtonsoft.Json;
  157. using DSharpPlus;
  158. using DSharpPlus.CommandsNext;
  159. using DSharpPlus.AsyncEvents;
  160. using DSharpPlus.Interactivity;
  161. using DSharpPlus.EventArgs;
  162. using DSharpPlus.Interactivity.Extensions;
  163. using BotName.Bot.Commands.Fun.SlashCommands;
  164. using DSharpPlus.CommandsNext.Exceptions;
  165. using DSharpPlus.CommandsNext.Attributes;
  166. using DSharpPlus.Entities;
  167. using System.Reflection.Emit;
  168. using Microsoft.Extensions.Hosting;
  169. using Microsoft.Extensions.DependencyInjection;
  170. using DSharpPlus.SlashCommands;
  171. using DSharpPlus.VoiceNext;`
  172.  
  173. namespace BotName
  174. {
  175.     public sealed class Program
  176.     {
  177.         public static DiscordClient Client { get; private set; }
  178.         public static InteractivityExtension Interactivity { get; private set; }
  179.         public static CommandsNextExtension Commands { get; private set; }
  180.         public static VoiceNextExtension VoiceNext { get; private set; }
  181.  
  182.  
  183.         static async Task Main(string[] args)
  184.         {
  185.  
  186.             //Main Window configs specifying the title name and color.
  187.             Console.BackgroundColor = ConsoleColor.Black;
  188.             Console.ForegroundColor = ConsoleColor.Magenta;
  189.             Console.Title = "BotName";
  190.  
  191.             //1. Get the details of your config.json file by deserialising it
  192.             var configJsonFile = new JSONReader();
  193.             await configJsonFile.ReadJSON();
  194.  
  195.             //2. Setting up the Bot Configuration
  196.             var discordConfig = new DiscordConfiguration()
  197.             {
  198.                 Intents = DiscordIntents.All,
  199.                 Token = configJsonFile.token,
  200.                 TokenType = TokenType.Bot,
  201.                 AutoReconnect = true
  202.             };
  203.  
  204.             //3. Apply this config to our DiscordClient
  205.             Client = new DiscordClient(discordConfig);
  206.  
  207.             //4. Set the default timeout for Commands that use interactivity
  208.             Client.UseInteractivity(new InteractivityConfiguration()
  209.             {
  210.                 Timeout = TimeSpan.FromMinutes(2)
  211.             });
  212.  
  213.             //5. Set up the Task Handler Ready event
  214.             Client.Ready += OnClientReady;
  215.  
  216.             //6. Set up the Commands Configuration
  217.             var commandsConfig = new CommandsNextConfiguration()
  218.             {
  219.                 StringPrefixes = new string[] { configJsonFile.prefix },
  220.                 EnableMentionPrefix = true,
  221.                 EnableDms = true,
  222.                 EnableDefaultHelp = false,
  223.             };
  224.  
  225.             Commands = Client.UseCommandsNext(commandsConfig);
  226.  
  227.             //7. Register your commands
  228.             var slashCommandsConfig = Client.UseSlashCommands();
  229.             slashCommandsConfig.RegisterCommands<MySoundsCommand>(MyGuildID);
  230.  
  231.             //8. Allows usage of voice channels.
  232.             var VoiceNext = Client.UseVoiceNext();
  233.  
  234.             //9. Connect to get the Bot online
  235.             await Client.ConnectAsync();
  236.             await Task.Delay(-1);
  237.         }
  238.  
  239.         private static Task OnClientReady(DiscordClient sender, ReadyEventArgs e)
  240.         {
  241.             return Task.CompletedTask;
  242.         }
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement