2015-12-05 10:27:00 +00:00
using Discord ;
using System ;
using System.IO ;
using Newtonsoft.Json ;
using Parse ;
2015-12-09 20:47:02 +00:00
using Discord.Commands ;
2015-12-05 10:27:00 +00:00
using NadekoBot.Modules ;
2015-12-09 20:47:02 +00:00
using Discord.Modules ;
2015-12-31 19:40:09 +00:00
using Discord.Audio ;
2016-01-21 02:17:01 +00:00
using NadekoBot.Extensions ;
2016-01-14 13:33:16 +00:00
using System.Timers ;
2016-02-06 13:43:03 +00:00
using System.Linq ;
2015-12-05 10:27:00 +00:00
2016-01-26 20:42:22 +00:00
namespace NadekoBot {
class NadekoBot {
2015-12-05 10:27:00 +00:00
public static DiscordClient client ;
public static string botMention ;
2016-01-11 19:35:49 +00:00
public static string GoogleAPIKey = null ;
2015-12-15 08:28:26 +00:00
public static ulong OwnerID ;
2016-01-22 05:35:44 +00:00
public static User OwnerUser = null ;
2015-12-30 04:44:36 +00:00
public static string password ;
2016-01-17 21:24:51 +00:00
public static string TrelloAppKey ;
2016-01-22 05:35:44 +00:00
public static bool ForwardMessages = false ;
2016-02-02 14:53:01 +00:00
public static Credentials creds ;
2016-02-04 18:02:08 +00:00
public static bool ParseActive = false ;
2015-12-05 10:27:00 +00:00
2016-01-26 20:42:22 +00:00
static void Main ( ) {
2015-12-05 10:27:00 +00:00
//load credentials from credentials.json
2016-01-22 07:17:16 +00:00
bool loadTrello = false ;
2016-01-26 20:42:22 +00:00
try {
2016-02-02 14:53:01 +00:00
creds = JsonConvert . DeserializeObject < Credentials > ( File . ReadAllText ( "credentials.json" ) ) ;
botMention = creds . BotMention ;
if ( string . IsNullOrWhiteSpace ( creds . GoogleAPIKey ) ) {
2016-01-11 19:35:49 +00:00
Console . WriteLine ( "No google api key found. You will not be able to use music and links won't be shortened." ) ;
2016-01-11 20:37:48 +00:00
} else {
2016-01-21 22:22:55 +00:00
Console . WriteLine ( "Google API key provided." ) ;
2016-02-02 14:53:01 +00:00
GoogleAPIKey = creds . GoogleAPIKey ;
2016-01-11 19:35:49 +00:00
}
2016-02-02 14:53:01 +00:00
if ( string . IsNullOrWhiteSpace ( creds . TrelloAppKey ) ) {
2016-01-17 21:24:51 +00:00
Console . WriteLine ( "No trello appkey found. You will not be able to use trello commands." ) ;
} else {
2016-01-21 22:22:55 +00:00
Console . WriteLine ( "Trello app key provided." ) ;
2016-02-02 14:53:01 +00:00
TrelloAppKey = creds . TrelloAppKey ;
2016-01-22 07:17:16 +00:00
loadTrello = true ;
2016-01-17 21:24:51 +00:00
}
2016-02-02 14:53:01 +00:00
if ( creds . ForwardMessages ! = true )
2016-01-22 05:35:44 +00:00
Console . WriteLine ( "Not forwarding messages." ) ;
else {
ForwardMessages = true ;
Console . WriteLine ( "Forwarding messages." ) ;
}
2016-02-02 14:53:01 +00:00
if ( string . IsNullOrWhiteSpace ( creds . ParseID ) | | string . IsNullOrWhiteSpace ( creds . ParseKey ) ) {
2016-02-06 13:43:03 +00:00
Console . WriteLine ( "Parse key and/or ID not found. Some functionality will be missing." ) ;
2016-02-04 18:02:08 +00:00
ParseActive = false ;
} else ParseActive = true ;
2016-02-02 14:53:01 +00:00
if ( string . IsNullOrWhiteSpace ( creds . OsuApiKey ) )
Console . WriteLine ( "No osu API key found. Osu functionality is disabled." ) ;
else
Console . WriteLine ( "Osu enabled." ) ;
2016-02-04 17:24:06 +00:00
if ( string . IsNullOrWhiteSpace ( creds . SoundCloudClientID ) )
Console . WriteLine ( "No soundcloud Client ID found. Soundcloud streaming is disabled." ) ;
else
Console . WriteLine ( "SoundCloud streaming enabled." ) ;
2016-01-30 11:08:30 +00:00
//init parse
2016-02-05 14:19:10 +00:00
if ( ParseActive )
try {
ParseClient . Initialize ( creds . ParseID , creds . ParseKey ) ;
} catch ( Exception ) { Console . WriteLine ( "Parse exception. Probably wrong parse credentials." ) ; }
2016-01-22 05:35:44 +00:00
2016-02-02 14:53:01 +00:00
OwnerID = creds . OwnerID ;
password = creds . Password ;
2016-01-26 20:42:22 +00:00
} catch ( Exception ex ) {
2016-01-30 11:08:30 +00:00
Console . WriteLine ( $"Failed to load stuff from credentials.json, RTFM\n{ex.Message}" ) ;
2015-12-05 10:27:00 +00:00
Console . ReadKey ( ) ;
return ;
}
//create new discord client
2016-01-11 19:35:49 +00:00
client = new DiscordClient ( ) ;
2015-12-05 10:27:00 +00:00
//create a command service
2016-01-26 20:42:22 +00:00
var commandService = new CommandService ( new CommandServiceConfig {
2015-12-05 10:27:00 +00:00
CommandChar = null ,
HelpMode = HelpMode . Disable
} ) ;
2016-01-30 11:08:30 +00:00
2016-01-22 05:35:44 +00:00
//reply to personal messages and forward if enabled.
2016-01-14 13:33:16 +00:00
client . MessageReceived + = Client_MessageReceived ;
2016-01-29 12:28:51 +00:00
2015-12-05 10:27:00 +00:00
//add command service
2015-12-30 04:44:36 +00:00
var commands = client . Services . Add < CommandService > ( commandService ) ;
2016-01-26 20:42:22 +00:00
2015-12-05 10:27:00 +00:00
//create module service
2015-12-30 04:44:36 +00:00
var modules = client . Services . Add < ModuleService > ( new ModuleService ( ) ) ;
2015-12-05 10:27:00 +00:00
2015-12-31 19:40:09 +00:00
//add audio service
var audio = client . Services . Add < AudioService > ( new AudioService ( new AudioServiceConfig ( ) {
2016-01-05 04:52:11 +00:00
Channels = 2 ,
2016-01-22 05:35:44 +00:00
EnableEncryption = false ,
2016-01-28 03:38:26 +00:00
EnableMultiserver = true ,
Bitrate = 128 ,
2015-12-31 19:40:09 +00:00
} ) ) ;
2015-12-05 10:27:00 +00:00
//install modules
2016-01-19 05:06:20 +00:00
modules . Add ( new Administration ( ) , "Administration" , ModuleFilter . None ) ;
modules . Add ( new Conversations ( ) , "Conversations" , ModuleFilter . None ) ;
modules . Add ( new Gambling ( ) , "Gambling" , ModuleFilter . None ) ;
modules . Add ( new Games ( ) , "Games" , ModuleFilter . None ) ;
modules . Add ( new Music ( ) , "Music" , ModuleFilter . None ) ;
modules . Add ( new Searches ( ) , "Searches" , ModuleFilter . None ) ;
2016-01-26 20:42:22 +00:00
if ( loadTrello )
2016-01-19 05:06:20 +00:00
modules . Add ( new Trello ( ) , "Trello" , ModuleFilter . None ) ;
2015-12-05 10:27:00 +00:00
//run the bot
2016-01-26 20:42:22 +00:00
client . ExecuteAndWait ( async ( ) = > {
2016-02-02 14:53:01 +00:00
await client . Connect ( creds . Username , creds . Password ) ;
2016-01-22 07:17:16 +00:00
Console . WriteLine ( "-----------------" ) ;
2016-01-30 08:01:48 +00:00
Console . WriteLine ( NadekoStats . Instance . GetStats ( ) ) ;
2016-01-22 07:17:16 +00:00
Console . WriteLine ( "-----------------" ) ;
2016-01-22 05:35:44 +00:00
foreach ( var serv in client . Servers ) {
if ( ( OwnerUser = serv . GetUser ( OwnerID ) ) ! = null )
return ;
}
2016-02-02 12:18:13 +00:00
client . ClientAPI . SendingRequest + = ( s , e ) = >
{
var request = e . Request as Discord . API . Client . Rest . SendMessageRequest ;
if ( request ! = null ) {
if ( string . IsNullOrWhiteSpace ( request . Content ) )
e . Cancel = true ;
}
} ;
2015-12-05 10:27:00 +00:00
} ) ;
Console . WriteLine ( "Exiting..." ) ;
Console . ReadKey ( ) ;
2016-01-30 08:01:48 +00:00
}
2016-01-22 07:17:16 +00:00
static bool repliedRecently = false ;
2016-01-14 13:33:16 +00:00
private static async void Client_MessageReceived ( object sender , MessageEventArgs e ) {
2016-01-22 07:17:16 +00:00
if ( e . Server ! = null | | e . User . Id = = client . CurrentUser . Id ) return ;
2016-02-06 13:43:03 +00:00
if ( PollCommand . ActivePolls . SelectMany ( kvp = > kvp . Key . Users . Select ( u = > u . Id ) ) . Contains ( e . User . Id ) ) return ;
2016-01-23 17:05:10 +00:00
//just ban this trash AutoModerator
if ( e . User . Id = = 105309315895693312 )
return ; // FU
2016-01-15 19:54:57 +00:00
try {
2016-01-23 16:21:50 +00:00
await ( await client . GetInvite ( e . Message . Text ) ) . Accept ( ) ;
await e . Send ( "I got in!" ) ;
return ;
} catch ( Exception ) {
if ( e . User . Id = = 109338686889476096 ) { //carbonitex invite
await e . Send ( "Failed to join the server." ) ;
return ;
}
}
2016-01-15 19:54:57 +00:00
2016-01-22 07:17:16 +00:00
if ( ForwardMessages & & OwnerUser ! = null )
2016-01-29 12:28:51 +00:00
await OwnerUser . SendMessage ( e . User + ": ```\n" + e . Message . Text + "\n```" ) ;
2016-01-14 13:33:16 +00:00
if ( repliedRecently = ! repliedRecently ) {
2016-01-30 04:24:32 +00:00
await e . Send ( "**COMMANDS DO NOT WORK IN PERSONAL MESSAGES**\nYou can type `-h` or `-help` or `@MyName help` in any of the channels I am in and I will send you a message with my commands.\n Or you can find out what i do here: https://github.com/Kwoth/NadekoBot\nYou can also just send me an invite link to a server and I will join it.\nIf you don't want me on your server, you can simply ban me ;(\nBot Creator's server: https://discord.gg/0ehQwTK2RBhxEi0X" ) ;
2016-01-14 13:33:16 +00:00
Timer t = new Timer ( ) ;
t . Interval = 2000 ;
t . Start ( ) ;
t . Elapsed + = ( s , ev ) = > {
repliedRecently = ! repliedRecently ;
t . Stop ( ) ;
t . Dispose ( ) ;
} ;
}
}
2015-12-05 10:27:00 +00:00
}
}