Skip to content

Commit

Permalink
add discord bot code (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh1ntra authored Oct 31, 2024
1 parent 29490ac commit ad54bd3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Content.Server/Administration/ServerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ await RunOnMainThread(async () =>
});
}

/*private async Task ActionAhelpSend(IStatusHandlerContext context, Actor actor)
private async Task ActionAhelpSend(IStatusHandlerContext context, Actor actor)
{
var body = await ReadJson<DiscordAhelpBody>(context);
if (body == null)
Expand All @@ -408,19 +408,19 @@ await RunOnMainThread(async () =>
await context.RespondErrorAsync(HttpStatusCode.BadRequest);
return;
}
//var _bwoinkSystem = _entitySystemManager.GetEntitySystem<BwoinkSystem>();
var _bwoinkSystem = _entitySystemManager.GetEntitySystem<BwoinkSystem>();
var playerUserId = new NetUserId(body.UserId);
var senderUserId = new NetUserId(actor.Guid);
var message = new SharedBwoinkSystem.BwoinkTextMessage(playerUserId,senderUserId, body.Text);
await RunOnMainThread(async () =>
{
if (_playerManager.TryGetSessionById(playerUserId, out var session))
{
//_bwoinkSystem.DiscordAhelpSendMessage(message, new EntitySessionEventArgs(session));
_bwoinkSystem.DiscordAhelpSendMessage(message, new EntitySessionEventArgs(session));
await RespondOk(context);
}
});
}*/
}

private async Task ShutdownAction(IStatusHandlerContext context, Actor actor)
{
Expand Down
59 changes: 59 additions & 0 deletions Content.Server/Administration/Systems/BwoinkSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,5 +517,64 @@ private static string GenerateAHelpMessage(string username, string message, bool
stringbuilder.Append(message);
return stringbuilder.ToString();
}
public void DiscordAhelpSendMessage(BwoinkTextMessage message, EntitySessionEventArgs eventArgs)
{
var senderSession = eventArgs.SenderSession;
// Confirm that this person is actually allowed to send a message here.
var personalChannel = senderSession.UserId == message.UserId;
var senderAdmin = _adminManager.GetAdminData(senderSession);
var senderAHelpAdmin = senderAdmin?.HasFlag(AdminFlags.Adminhelp) ?? false;
var authorized = personalChannel || senderAHelpAdmin;
if (!authorized)
{
// Unauthorized bwoink (log?)
return;
}
var escapedText = FormattedMessage.EscapeText(message.Text);
string bwoinkText;
if (senderAdmin is not null && senderAdmin.Flags == AdminFlags.Adminhelp) // Mentor. Not full admin. That's why it's colored differently.
{
bwoinkText = $"[color=purple]{senderSession.Name}[/color]";
}
else if (senderAdmin is not null && senderAdmin.HasFlag(AdminFlags.Adminhelp))
{
bwoinkText = $"[color=red]{senderSession.Name}[/color]";
}
else
{
bwoinkText = $"{senderSession.Name}";
}
bwoinkText = $"{"(Discord) "}{bwoinkText}: {escapedText}";
// If it's not an admin / admin chooses to keep the sound then play it.
var playSound = !senderAHelpAdmin || message.PlaySound;
var msg = new BwoinkTextMessage(message.UserId, senderSession.UserId, bwoinkText, playSound: playSound);
LogBwoink(msg);
var admins = GetTargetAdmins();
// Notify all admins
foreach (var channel in admins)
{
RaiseNetworkEvent(msg, channel);
}
var sendsWebhook = _webhookUrl != string.Empty;
if (sendsWebhook)
{
if (!_messageQueues.ContainsKey(msg.UserId))
_messageQueues[msg.UserId] = new Queue<string>();
var str = message.Text;
var unameLength = senderSession.Name.Length;
if (unameLength + str.Length + _maxAdditionalChars > DescriptionMax)
{
str = str[..(DescriptionMax - _maxAdditionalChars - unameLength)];
}
var nonAfkAdmins = GetNonAfkAdmins();
_messageQueues[msg.UserId].Enqueue(GenerateAHelpMessage(senderSession.Name, str, !personalChannel, _gameTicker.RoundDuration().ToString("hh\\:mm\\:ss"), _gameTicker.RunLevel, playedSound: playSound, noReceivers: nonAfkAdmins.Count == 0));
}
if (admins.Count != 0 || sendsWebhook)
return;
// No admin online, let the player know
var systemText = Loc.GetString("bwoink-system-starmute-message-no-other-users");
var starMuteMsg = new BwoinkTextMessage(message.UserId, SystemUserId, systemText);
RaiseNetworkEvent(starMuteMsg, senderSession.Channel);
}
}
}

0 comments on commit ad54bd3

Please sign in to comment.