C#:
// Все права принадлежат дискорд сообществу https://discord.gg/VgNHPpNrz6
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Oxide.Plugins
{
[Info("TwitchAuth", "Murky", "0.0.1", ResourceId = 1838)]
[Description("Only allow your followers on Twitch to join your server")]
class TwitchAuth : RustPlugin
{
// Do NOT edit this file, instead edit TwitchAuth.json in oxide/config and TwitchAuth.en.json in the oxide/lang directory,
// or create a new language file for another language using the 'en' file as a default.public class TwitchUser
{
public int Id { get; set; }
public string Name { get; set; }
}
bool adminExcluded;
string twitchAccount;
protected override void LoadDefaultConfig()
{
Config["AdminExcluded"] = adminExcluded = GetConfig("AdminExcluded", true);
Config["TwitchAccount"] = twitchAccount = GetConfig("TwitchAccount", "YOUR_TWITCH_ACCOUNT_NAME");
SaveConfig();
}
void Loaded()
{
LoadDefaultConfig();
lang.RegisterMessages(new Dictionary<string, string>
{
{"RejectMessage", "Please follow @ twitch.tv/{0} and connect Twitch to Steam to join" },
{"UsageMessage","Usage:\nta.account TwitchUserHere" },
{"AccountSetMessage","Account set: {0}" },
{"NotAllowedMessage","You are not allowed to use this command." }
}, this);
}
void OnUserApprove(Network.Connection connection)
{
if (adminExcluded && IsAdmin(connection))
{
Puts($"{connection.username} is excluded from Twitch check");
return;
}
UserHasSteam(connection);
}
[ConsoleCommand("ta.account")]
void TwitchAuthSetAccountCommand(ConsoleSystem.Arg arg)
{
if (arg.connection != null)
{
var userId = arg.connection.userid;
if (arg.connection.authLevel >= 1)
{
if (arg.Args == null || arg.Args.Length < 1)
{
SendReply(arg, lang.GetMessage("UsageMessage", this, userId));
}
else
{
Config["TwitchAccount"] = twitchAccount = arg.Args[0];
SaveConfig();
SendReply(arg, string.Format(lang.GetMessage("AccountSetMessage", this, userId), arg.Args[0]));
}
}
else
{
SendReply(arg, lang.GetMessage("NotAllowedMessage", this, userId));
}
}
}
void UserHasSteam(Network.Connection connection)
{
webrequest.EnqueueGet($"https://api.twitch.tv/api/steam/{connection.userid}", (code, response) =>
UserHasSteamConnectedCallBack(code, response, connection), this);
}
void UserHasSteamConnectedCallBack(int code, string response, Network.Connection connection)
{
if (code != 200)
{
Puts($"{connection.username} tried to log in but does not have its Steam account connected to Twitch");
ConnectionAuth.Reject(connection, string.Format(lang.GetMessage("RejectMessage", this, connection.userid.ToString()), twitchAccount));
}
else
{
var user = JsonConvert.DeserializeObject<TwitchUser>(response);
if (!string.IsNullOrEmpty(user?.Name))
{
Puts($"{connection.username} connecting.Checking if is a follower.");
UserIsFollowing(user.Name, connection);
}
}
}
void UserIsFollowing(string name, Network.Connection connection)
{
webrequest.EnqueueGet($"https://api.twitch.tv/kraken/users/{name}/follows/channels/{twitchAccount}", (code, response) =>
UserIsFollowingCallBack(code, response, connection), this);
}
void UserIsFollowingCallBack(int code, string response, Network.Connection connection)
{
if (code != 200)
{
Puts($"{connection.username} tried to log in but does not follow you on Twitch");
ConnectionAuth.Reject(connection, string.Format(lang.GetMessage("RejectMessage", this, connection.userid.ToString()), twitchAccount));
}
else
{
Puts($"{connection.username} is a follower.Allowing.");
}
}
T GetConfig<T>(string name, T defaultValue)
{
if (Config[name] == null) return defaultValue;
return (T)Convert.ChangeType(Config[name], typeof(T));
}
bool IsAdmin(Network.Connection connection) => permission.UserHasGroup(connection.userid.ToString(), "admin") || connection.authLevel > 0;
}
}