immcrab/GorillaStreak
Ownership not verified 2
Viewing main at commit 59428b41fa“Update README.md”· 11 months ago
146 lines · 4.2 KBRaw on GitHub
using BepInEx;
using GorillaLocomotion;
using Photon.Pun;
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
[BepInPlugin("com.imcrab.gorillatag.gorillastreak", "GorillaStreak", "1.0.0")]
public class GorillaStreak : BaseUnityPlugin
{
private string pluginFolder;
private string webhookFile;
private string streakFile;
private string messageFile;
private string webhookUrl;
private string customMessageTemplate;
private int streakDay = 1;
private DateTime lastPlayed;
private void Awake()
{
pluginFolder = Path.Combine(Paths.PluginPath, "GorillaStreak");
webhookFile = Path.Combine(pluginFolder, "webhook.txt");
streakFile = Path.Combine(pluginFolder, "streak.json");
messageFile = Path.Combine(pluginFolder, "message.txt");
Directory.CreateDirectory(pluginFolder);
if (!File.Exists(webhookFile))
File.WriteAllText(webhookFile, "replace this with your Discord webhook");
webhookUrl = File.ReadAllText(webhookFile).Trim();
string defaultMessage = "You can use this, to make a custom message! Use variables like, {player} and {streak} to use the player name, and how many days. Delete this and replace with your message.";
if (!File.Exists(messageFile))
File.WriteAllText(messageFile, defaultMessage);
customMessageTemplate = File.ReadAllText(messageFile).Trim();
if (customMessageTemplate.StartsWith("//") || string.IsNullOrEmpty(customMessageTemplate))
{
customMessageTemplate = "{player} has started their streak! This is day {streak}.";
}
LoadStreak();
UpdateStreak();
SaveStreak();
}
private void Start()
{
StartCoroutine(WaitForPhotonAndSend());
}
private System.Collections.IEnumerator WaitForPhotonAndSend()
{
while (!PhotonNetwork.IsConnectedAndReady)
{
yield return null;
}
string playerName = PhotonNetwork.NickName ?? "Unknown Player";
if (string.IsNullOrEmpty(playerName))
playerName = "Unknown Player";
string message = customMessageTemplate
.Replace("{player}", playerName)
.Replace("{streak}", streakDay.ToString());
if (!string.IsNullOrEmpty(webhookUrl) && webhookUrl.StartsWith("https://"))
SendWebhook(message).Forget();
}
private void LoadStreak()
{
try
{
if (File.Exists(streakFile))
{
string[] data = File.ReadAllText(streakFile).Split('|');
streakDay = int.Parse(data[0]);
lastPlayed = DateTime.Parse(data[1]);
}
else
{
streakDay = 0;
lastPlayed = DateTime.MinValue;
}
}
catch
{
streakDay = 0;
lastPlayed = DateTime.MinValue;
}
}
private void SaveStreak()
{
File.WriteAllText(streakFile, $"{streakDay}|{DateTime.Now.ToShortDateString()}");
}
private void UpdateStreak()
{
if (lastPlayed.Date == DateTime.Now.Date)
{
return;
}
else if (lastPlayed.Date == DateTime.Now.AddDays(-1).Date)
{
streakDay++;
}
else
{
streakDay = 1;
}
lastPlayed = DateTime.Now;
}
private async Task SendWebhook(string message)
{
try
{
using (HttpClient client = new HttpClient())
{
var json = "{\"content\": \"" + message.Replace("\"", "\\\"") + "\"}";
var content = new StringContent(json, Encoding.UTF8, "application/json");
await client.PostAsync(webhookUrl, content);
}
}
catch (Exception ex)
{
Logger.LogError("Failed to send webhook: " + ex.Message);
}
}
}
public static class TaskExtensions
{
public static void Forget(this Task task) { }
}Code is shown from GitHub as-is. Linking a repository doesn’t prove that a release file on Canopy was built from it.