Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C# by ertyuilkl ( 8 years ago )
//Program.cs file:
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using discordBot.Resources.Core;
using discordBot.Resources.Settings;
namespace discordBot
{
class Program
{
private DiscordSocketClient Client;
private CommandService Commands;
static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
private async Task MainAsync()
{
string JSON = "";
string SettingsLocation = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location).Replace(@"bin\Debug\netcoreapp2.0", @"config\settings.json");
using (var Stream = new FileStream(SettingsLocation, FileMode.Open, FileAccess.Read))
using (var ReadSettings = new StreamReader(Stream))
{
JSON = ReadSettings.ReadToEnd();
}
Setting Settings = JsonConvert.DeserializeObject<Setting>(JSON);
ESettings.Token = Settings.token;
Client = new DiscordSocketClient(new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug
});
//help
Commands = new CommandService(new CommandServiceConfig
{
CaseSensitiveCommands = true,
DefaultRunMode = RunMode.Async,
LogLevel = LogSeverity.Debug
});
Client.MessageReceived += Client_MessageReceived;
await Commands.AddModulesAsync(Assembly.GetEntryAssembly());
Client.Ready += Client_Ready;
Client.Log += Client_Log;
await Client.LoginAsync(TokenType.Bot, Settings.token);
await Client.StartAsync();
await Task.Delay(-1);
}
//Show log
private async Task Client_Log(LogMessage Message)
{
Console.WriteLine($"{DateTime.Now} at {Message.Source}] {Message.Message}");
}
//Set game
private async Task Client_Ready()
{
//await Client.SetGameAsync("test bot", "https://www.google.com", StreamType.NotStreaming);
await Client.SetGameAsync("discordBot");
}
private async Task Client_MessageReceived(SocketMessage MessageParam)
{
//Load commands
var Message = MessageParam as SocketUserMessage;
var Context = new SocketCommandContext(Client, Message);
if (Context.Message == null || Context.Message.Content == "") return;
if (Context.User.IsBot) return;
int ArgPos = 0;
if (!(Message.HasStringPrefix("!", ref ArgPos) || Message.HasMentionPrefix(Client.CurrentUser, ref ArgPos))) return;
var Result = await Commands.ExecuteAsync(Context, ArgPos);
if (!Result.IsSuccess)
{
string username = Regex.Replace(Context.User.Mention, "[^0-9]", "");
Console.WriteLine($"{DateTime.Now} at Commands] UserID: {username} | Command: {Context.Message.Content} | Error: {Result.ErrorReason}");
await Context.Channel.SendMessageAsync("Command not found. Type !help for more info.");
}
Console.WriteLine(Environment.CurrentDirectory);
}
}
}
//ESettings.cs file:
using System;
using System.Collections.Generic;
using System.Text;
namespace discordBot.Resources.Settings
{
public class ESettings
{
public static string Token;
}
}
//Core.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace discordBot.Resources.Core
{
//Settings
public class Setting
{
public string token { get; set; }
}
}
//settings file
{
"token": "test"
}
Revise this Paste