Skip to content

Commit

Permalink
YouTube.cs Information.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan23a committed Oct 8, 2017
1 parent 9c8ea5e commit daea8ec
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 7 deletions.
1 change: 1 addition & 0 deletions DracosBot/DracosBot/DracosBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<ItemGroup>
<Compile Include="9gagCommand.cs" />
<Compile Include="Command.cs" />
<Compile Include="Information.cs" />
<Compile Include="PingCommand.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
64 changes: 64 additions & 0 deletions DracosBot/DracosBot/Information.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Newtonsoft.Json;
using System.IO;

namespace DracosBot
{
internal static class Information
{
internal static Config Config => JsonConvert.DeserializeObject<Config>(File.ReadAllText("config.json"));

internal static string ClientId => Config.ClientId;
internal static string ClientSecret => Config.ClientSecret;
internal static string BotName => Config.BotName;
internal static string Token => Config.Token;
internal static string ServerName => Config.ServerName;
internal static string TextChannelName => Config.TextChannelName;
internal static string VoiceChannelName => Config.VoiceChannelName;
}

public class Config
{
public string ClientId = "YourClientID";
public string ClientSecret = "YourClientSecret";
public string BotName = "YourBotName";
public string Token = "YourBotToken";
public string ServerName = "TheServerYouWantToConnectTo";
public string TextChannelName = "TheTextChannelYouWantToJoin";
public string VoiceChannelName = "TheVoiceChannelYouWantToJoin";

public static bool operator ==(Config cfg1, Config cfg2)
{
return ReferenceEquals(cfg1, null) ? ReferenceEquals(cfg2, null) : cfg1.Equals(cfg2);
}

public static bool operator !=(Config cfg1, Config cfg2)
{
return !ReferenceEquals(cfg1, null) ? !ReferenceEquals(cfg2, null) : !cfg1.Equals(cfg2);
}

public bool Equals(Config compare)
{
if (compare == null)
return false;

return
ClientId == compare.ClientId &&
ClientSecret == compare.ClientSecret &&
BotName == compare.BotName &&
Token == compare.Token &&
ServerName == compare.ServerName &&
TextChannelName == compare.TextChannelName &&
VoiceChannelName == compare.VoiceChannelName;
}

public override bool Equals(object obj)
{
return Equals(obj as Config);
}

public override int GetHashCode()
{
return (ClientId + ClientSecret + BotName + Token + ServerName + TextChannelName + VoiceChannelName).GetHashCode();
}
}
}
95 changes: 88 additions & 7 deletions DracosBot/DracosBot/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
using System;
using Discord;
using Discord.Audio;
using Discord.Rest;
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Discord;
using Discord.WebSocket;

namespace DracosBot
{

class Program
class Program
{
private IVoiceChannel _voiceChannel;
private ITextChannel _textChannel;
private IAudioClient _audio;
private DiscordSocketClient _client;


Expand All @@ -32,6 +37,12 @@ public async Task MainAsync()
_client.Log += Log;
_client.MessageReceived += OnMessageReceived;

//-
_client.Disconnected += Disconnected;

//+
_client.Connected += Connected;
_client.Ready += OnReady;
//TOKEN STORING LOGIC
//Store the token in a txt file to avoid hardcoding it
//User confirmation needed
Expand Down Expand Up @@ -104,7 +115,7 @@ public async Task MainAsync()
//Block this task until the program is closed
await Task.Delay(-1);
}

//Pass MessageReceived Event on to Async Method (so nothing blocks)
private Task OnMessageReceived(SocketMessage message)
{
MessageReceived(message);
Expand All @@ -125,5 +136,75 @@ private Task Log(LogMessage msg)
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}

#region Events

//Connection Lost
private static Task Disconnected(Exception arg)
{
Print($"Connection lost! ({arg.Message})", ConsoleColor.Red);
return Task.CompletedTask;
}

//Connected
private static Task Connected()
{
Console.Title = "Music Bot (Connected)";

Print("Connected!", ConsoleColor.Green);
return Task.CompletedTask;
}


//Pass Ready Event on to Async Method (so nothing blocks)
private Task OnReady()
{
Ready();
return Task.CompletedTask;
}

//On Bot ready
private async void Ready()
{
Print("Ready!", ConsoleColor.Green);

//"Playing Nothing :/"
await _client.SetGameAsync("Nothing :/");

//Get Guilds / Servers
try
{
//Server
PrintServers();
SocketGuild guild = _client.Guilds.FirstOrDefault(g => g.Name == Information.ServerName);

//Text Channel
_textChannel = guild.TextChannels.FirstOrDefault(t => t.Name == Information.TextChannelName);
Print($"Using Text Channel: \"#{_textChannel.Name}\"", ConsoleColor.Cyan);

//Voice Channel
_voiceChannel = guild.VoiceChannels.FirstOrDefault(t => t.Name == Information.VoiceChannelName);
Print($"Using Voice Channel: \"{_voiceChannel.Name}\"", ConsoleColor.Cyan);
_audio = await _voiceChannel.ConnectAsync();
}
catch (Exception e)
{
Print("Could not join Voice/Text Channel (" + e.Message + ")", ConsoleColor.Red);
}
}
private void PrintServers()
{
//Print added Servers
Print("\n\rAdded Servers:", ConsoleColor.Cyan);
foreach (SocketGuild server in _client.Guilds)
{
Print(server.Name == Information.ServerName
? $" [x] {server.Name}"
: $" [ ] {server.Name}", ConsoleColor.Cyan);
}
Print("", ConsoleColor.Cyan);
}

}
}
#endregion
69 changes: 69 additions & 0 deletions DracosBot/DracosBot/YoutubeCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Discord;
using Discord.Audio;
using Discord.WebSocket;
using Discord.Rest;
namespace DracosBot
{
internal class YoutubeCommand : Program
{
private DiscordSocketClient _clientThis;
private IVoiceChannel _voiceChannel;
private ITextChannel _textChannel;
private TaskCompletionSource<bool> _tsc;
private IAudioClient _audio;
private CancellationTokenSource _disposeToken;
private Queue<Tuple<string, string, string, string>> _queue;

public static string[] command = { "!youtube" };



/// <summary>
/// Tuple(FilePath, Video Name, Duration, Requested by)
/// </summary>
private bool Pause
{
get => _internal_Pause;
set
{
new Thread(() => _tsc.TrySetResult(value)).Start();
_internal_Pause = value;
}
}
private bool _internal_Pause;
private bool Skip
{
get
{
bool ret = _internal_Skip;
_internal_Skip = false;
return ret;
}
set => _internal_Skip = value;
}
private bool _internal_Skip;

public bool isDisposed;

public YoutubeCommand()
{
Initialize();
}
public async void Initialize()
{
_queue = new Queue<Tuple<string, string, string, string>>();
_tsc = new TaskCompletionSource<bool>();
_disposeToken = new CancellationTokenSource();
_clientThis = _client;
}

}
}

0 comments on commit daea8ec

Please sign in to comment.