Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evandixon committed Aug 2, 2017
1 parent c5ac40d commit 6e0a485
Show file tree
Hide file tree
Showing 51 changed files with 11,034 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
28 changes: 28 additions & 0 deletions Ditto.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.15
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ditto", "Ditto\Ditto.csproj", "{B4D93EE6-97DA-43DD-84CB-DCDC3E843BC8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IrcDotNet", "IrcDotNet\IrcDotNet.csproj", "{5A8EF7A9-4DFB-4DA6-A73B-C0028E26DBFA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B4D93EE6-97DA-43DD-84CB-DCDC3E843BC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4D93EE6-97DA-43DD-84CB-DCDC3E843BC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4D93EE6-97DA-43DD-84CB-DCDC3E843BC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4D93EE6-97DA-43DD-84CB-DCDC3E843BC8}.Release|Any CPU.Build.0 = Release|Any CPU
{5A8EF7A9-4DFB-4DA6-A73B-C0028E26DBFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A8EF7A9-4DFB-4DA6-A73B-C0028E26DBFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A8EF7A9-4DFB-4DA6-A73B-C0028E26DBFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A8EF7A9-4DFB-4DA6-A73B-C0028E26DBFA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
16 changes: 16 additions & 0 deletions Ditto/Ditto.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IrcDotNet\IrcDotNet.csproj" />
</ItemGroup>

</Project>
200 changes: 200 additions & 0 deletions Ditto/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
using Discord;
using Discord.WebSocket;
using IrcDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ditto
{
public class Program
{
private static string _discordToken;
private static ulong _discordGuildId;
private static ulong _discordChannelId;
private static DiscordSocketClient _discordClient;

private static IrcDotNet.StandardIrcClient _ircClient;
private static Uri _ircServer;
private static IrcDotNet.IrcUserRegistrationInfo _ircRegInfo;
private static string _ircChannel;

private static bool _listen;

public static void Main(string[] args)
{
_listen = true;

_discordToken = "token";
_discordGuildId = 342007647702220800;
_discordChannelId = 342032979327057922;

_ircServer = new Uri("irc://irc.projectpokemon.org:6667/ProjectPokemon");
_ircRegInfo = new IrcUserRegistrationInfo();
_ircRegInfo.NickName = "Ditto";
_ircRegInfo.UserName = "Ditto";
_ircRegInfo.Password = "password";
_ircRegInfo.RealName = "Ditto";
_ircChannel = "#pp-test";

MainAsync().GetAwaiter().GetResult();
}

public static async Task MainAsync()
{
// IRC
_ircClient = new IrcDotNet.StandardIrcClient();
_ircClient.RawMessageReceived += Irc_RawMessageReceived;
_ircClient.ErrorMessageReceived += Irc_ErrorMessageReceived;
_ircClient.Connected += Irc_Connected;
_ircClient.ConnectFailed += Irc_ConnectFailed;
_ircClient.MotdReceived += Irc_MotdReceived;
_ircClient.Connect("irc.projectpokemon.org", 6667, false, _ircRegInfo);

// Discord
_discordClient = new DiscordSocketClient();
_discordClient.Log += Discord_Log;
_discordClient.MessageReceived += Discord_MessageReceived;
await _discordClient.LoginAsync(TokenType.Bot, _discordToken);
await _discordClient.StartAsync();

// Listen for mannual commands
while (_listen)
{
var input = Console.ReadLine();
var cmd = input.Split(" ".ToCharArray(), 2);
switch (cmd[0].ToLower())
{
case "say":
if (cmd.Length > 1)
{
await SendDiscordMessage(cmd[1]);
await SendIrcMessage(cmd[1]);
}
else
{
Console.WriteLine("Usage: say <message>");
}
break;
case "exit":
case "quit":
_listen = false;
break;
default:
break;
}
}
}

private static async Task SendDiscordMessage(string msg)
{
await _discordClient.GetGuild(_discordGuildId).GetTextChannel(_discordChannelId).SendMessageAsync(msg);
}

private static Task SendIrcMessage(string msg)
{
_ircClient.LocalUser.SendMessage("#pp-test", msg);
return Task.CompletedTask;
}

private static Task Discord_Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}

private static async Task Discord_MessageReceived(SocketMessage message)
{
if (message.Channel.Id != _discordChannelId || message.Author.Username == _discordClient.CurrentUser.Username)
{
return;
}

Console.WriteLine($"#{message.Channel.Name}: [{message.Author.Username}] {message.Content}");

if (message.Content == "!ping")
{
await message.Channel.SendMessageAsync("Pong!");
}
else
{
var lines = message.Content.Split('\n').Select(x => x.Trim()).ToArray();
for (int i = 0; i < Math.Min(lines.Length, 4); i++)
{
await SendIrcMessage($"<{message.Author.Username}> {lines[i]}");
}
if (lines.Length > 4)
{
await SendIrcMessage($"({message.Author.Username} posted {lines.Length - 5} more lines not shown here)");
}
}
}

private static void Irc_ChannelMessageReceived(object sender, IrcMessageEventArgs e)
{
if (!e.Targets.Any(x => x.Name == _ircChannel) || e.Source.Name == _ircClient.LocalUser.NickName)
{
return;
}

if (e.Text == "!ping")
{
SendIrcMessage("Pong!");
}
else if (e.Text.StartsWith((char)1 + "ACTION ") && e.Text.Last() == (char)1)
{
// The /me command
SendDiscordMessage($"**{e.Source}** {e.Text.Substring(8, e.Text.Length - 8)}").Wait();
}
else
{
SendDiscordMessage($"<**{e.Source}**> {e.Text}").Wait();
}
}

private static void Irc_RawMessageReceived(object sender, IrcRawMessageEventArgs e)
{
Console.WriteLine(e.RawContent);
}

private static void Irc_ErrorMessageReceived(object sender, IrcErrorMessageEventArgs e)
{
Console.WriteLine(e.Message);
}

private static void Irc_Connected(object sender, EventArgs e)
{
Console.WriteLine("Connected to IRC");
}

private static void Irc_MotdReceived(object sender, EventArgs e)
{
Console.WriteLine("Motd received");
_ircClient.LocalUser.JoinedChannel += Irc_JoinedChannel;
_ircClient.LocalUser.LeftChannel += Irc_LeftChannel;
_ircClient.Channels.Join(_ircChannel);
}

private static void Irc_JoinedChannel(object sender, IrcChannelEventArgs e)
{
Console.WriteLine("Joined channel");
e.Channel.MessageReceived += Irc_ChannelMessageReceived;
}

private static void Irc_LeftChannel(object sender, IrcChannelEventArgs e)
{
Console.WriteLine("Left channel");
e.Channel.MessageReceived -= Irc_ChannelMessageReceived;

// Try to join again
_ircClient.Channels.Join(_ircChannel);
}

private static void Irc_ConnectFailed(object sender, IrcErrorEventArgs e)
{
Console.WriteLine("Connect failed.");
Console.WriteLine(e.Error);
}
}
}
Loading

0 comments on commit 6e0a485

Please sign in to comment.