-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discord Bot, chat to players in game from discord channels
- Loading branch information
1 parent
995d68c
commit be22ece
Showing
16 changed files
with
338 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="ArchaicQuest-All" type="CompoundRunConfigurationType"> | ||
<toRun name="ArchaicQuestII.API" type="LaunchSettings" /> | ||
<toRun name="ArchaicQuestII.DiscordBot" type="DotNetProject" /> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using ArchaicQuestII.API.Controllers.Discord; | ||
using ArchaicQuestII.GameLogic.Client; | ||
using ArchaicQuestII.GameLogic.Core; | ||
using ArchaicQuestII.GameLogic.Utilities; | ||
using Discord; | ||
using Discord.WebSocket; | ||
using Microsoft.AspNetCore.SignalR; | ||
using Newtonsoft.Json; | ||
|
||
namespace ArchaicQuestII.DiscordBot; | ||
|
||
public class Bot | ||
{ | ||
private static ICache _cache; | ||
private static IHubContext<GameLogic.Hubs.GameHub> _hubContext; | ||
private DiscordSocketClient _client; | ||
private static readonly HttpClient httpClient = new HttpClient(); | ||
public Bot(ICache cache, IHubContext<GameLogic.Hubs.GameHub> hubContext, DiscordSocketClient client) | ||
{ | ||
_cache = cache; | ||
_hubContext = hubContext; | ||
_client = client; | ||
} | ||
|
||
public async Task MainAsync() | ||
{ | ||
var config = new DiscordSocketConfig | ||
{ | ||
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent | GatewayIntents.All | ||
}; | ||
_client = new DiscordSocketClient(config); | ||
_client.Log += Log; | ||
|
||
//TODO: Rename to Discord TOKEN here and in the admin project | ||
var token = _cache.GetConfig().ChannelDiscordWebHookURL; | ||
|
||
await _client.LoginAsync(TokenType.Bot, token); | ||
await _client.StartAsync(); | ||
|
||
_client.Ready += async () => | ||
{ | ||
_client.MessageCommandExecuted += MessageCommandHandler; | ||
_client.UserCommandExecuted += UserCommandHandler; | ||
_client.MessageReceived += ClientOnMessageReceived; | ||
|
||
}; | ||
// Block this task until the program is closed. | ||
await Task.Delay(-1); | ||
} | ||
public async Task UserCommandHandler(SocketUserCommand arg) | ||
{ | ||
Console.WriteLine("User command received!", arg.CommandName); | ||
} | ||
|
||
public async Task MessageCommandHandler(SocketMessageCommand arg) | ||
{ | ||
Console.WriteLine("Message command received!", arg.CommandName); | ||
} | ||
private async Task ClientOnMessageReceived(SocketMessage socketMessage) | ||
{ | ||
await Task.Run(async () => | ||
{ | ||
//Activity is not from a Bot. | ||
|
||
if (!socketMessage.Author.IsBot) | ||
{ | ||
|
||
var username = socketMessage.Author.Username; | ||
var authorId = socketMessage.Author.Id; | ||
var channelId = socketMessage.Channel.Id.ToString(); | ||
var messageId = socketMessage.Id; | ||
var message = socketMessage.Content; | ||
|
||
var channel = _client.GetChannel(Convert.ToUInt64(channelId)); | ||
var socketChannel = (ISocketMessageChannel)channel; | ||
|
||
var discordBotdata = new DiscordBotData() { Channel = "", Message = message, Username = username}; | ||
if (socketChannel.Name == "newbie-chat") | ||
{ | ||
discordBotdata.Channel = "newbie"; | ||
} | ||
|
||
if (socketChannel.Name == "ooc-chat") | ||
{ | ||
discordBotdata.Channel = "ooc"; | ||
} | ||
|
||
if (socketChannel.Name == "gossip-chat") | ||
{ | ||
discordBotdata.Channel = "gossip"; | ||
} | ||
|
||
if (!string.IsNullOrEmpty(discordBotdata.Channel)) | ||
{ | ||
await PostToNewbieChannel(discordBotdata); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public async Task<string> PostDataAsync(string url, DiscordBotData data) | ||
{ | ||
|
||
var jsonData = JsonConvert.SerializeObject(data); | ||
|
||
// create a StringContent object with the JSON data | ||
var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); | ||
|
||
// send a POST request using the static HttpClient instance | ||
var response = await httpClient.PostAsync(url, content); | ||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
return responseContent; | ||
} | ||
|
||
public async Task PostToNewbieChannel(DiscordBotData data) | ||
{ | ||
var message = $"<p class='newbie'>[<span>Newbie</span>] {data.Username}: {data.Message}</p>"; | ||
|
||
if (data.Channel == "ooc") | ||
{ | ||
message = $"<p class='ooc'>[<span>OOC</span>] {data.Username}: {data.Message}</p>"; | ||
} | ||
|
||
if (data.Channel == "gossip") | ||
{ | ||
message = $"<p class='gossip'>[<span>Gossip</span>] {data.Username}: {data.Message}</p>"; | ||
} | ||
|
||
foreach (var pc in _cache.GetAllPlayers().Where(x => x.Config.NewbieChannel)) | ||
{ | ||
await SendMessageToClient(message, pc.ConnectionId); | ||
await UpdateCommunicationAll(message, data.Channel, pc.ConnectionId); | ||
} | ||
|
||
} | ||
|
||
private async Task SendMessageToClient(string message, string id) | ||
{ | ||
try | ||
{ | ||
await _hubContext.Clients.Client(id).SendAsync("SendMessage", message, ""); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
} | ||
} | ||
|
||
public async Task UpdateCommunicationAll(string message, string type, string id) | ||
{ | ||
if (string.IsNullOrEmpty(message)) | ||
{ | ||
return; | ||
} | ||
try | ||
{ | ||
await _hubContext.Clients.Client(id).SendAsync("CommUpdate", message, type); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
} | ||
} | ||
|
||
private Task Log(LogMessage msg) | ||
{ | ||
Console.WriteLine(msg.ToString()); | ||
return Task.CompletedTask; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
ArchaicQuestII.API/Controllers/Discord/DiscordController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using ArchaicQuestII.DataAccess; | ||
using ArchaicQuestII.GameLogic.Character; | ||
using ArchaicQuestII.GameLogic.Character.Config; | ||
using ArchaicQuestII.GameLogic.Client; | ||
using ArchaicQuestII.GameLogic.Core; | ||
using ArchaicQuestII.GameLogic.Hubs; | ||
using Microsoft.AspNet.SignalR; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Newtonsoft.Json; | ||
|
||
namespace ArchaicQuestII.API.Controllers.Discord; | ||
|
||
public class DiscordBotData | ||
{ | ||
public string Channel { get; set; } | ||
public string Message { get; set; } | ||
public string Username { get; set; } | ||
} | ||
|
||
|
||
public class DiscordController : Controller | ||
{ | ||
//private IHubContext<GameHub> _gameHubContext; | ||
private ICore Core { get; } | ||
private ICache Cache { get; } | ||
public DiscordController(ICore core, ICache cache) | ||
{ | ||
Core = core; | ||
Cache = cache; | ||
} | ||
|
||
|
||
[HttpPost] | ||
[AllowAnonymous] | ||
[Route("api/discord/updateChannel")] | ||
public Task<IActionResult> Post([FromBody] DiscordBotData data) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
PostToNewbieChannel(data); | ||
|
||
} | ||
|
||
return Task.FromResult<IActionResult>(Ok()); | ||
} | ||
|
||
public void PostToNewbieChannel(DiscordBotData data) | ||
{ | ||
var message = $"<p class='newbie'>[<span>Newbie</span>] {data.Username}: {data.Message}</p>"; | ||
|
||
foreach (var pc in Core.Cache.GetAllPlayers().Where(x => x.Config.NewbieChannel)) | ||
{ | ||
Core.Writer.WriteLine(message, pc.ConnectionId); | ||
Core.UpdateClient.UpdateCommunication(pc, message, data.Channel); | ||
} | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"DiscordToken": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.