Add IBotFriendRequest plugin interface

This commit is contained in:
JustArchi 2019-01-31 04:45:17 +01:00
parent dd2b1bde25
commit 95356af9d3
4 changed files with 89 additions and 2 deletions

View file

@ -35,7 +35,7 @@ namespace ArchiSteamFarm.CustomPlugins.ExamplePlugin {
// Your plugin class should inherit the plugin interfaces it wants to handle
// If you do not want to handle a particular action (e.g. OnBotMessage that is offered in IBotMessage), it's the best idea to not inherit it at all
// This will keep your code compact, efficient and less dependent. You can always add additional interfaces when you'll need them, this example project will inherit quite a bit of them to show you potential usage
internal sealed class ExamplePlugin : IASF, IBot, IBotCommand, IBotConnection, IBotMessage, IBotModules, IBotTradeOffer {
internal sealed class ExamplePlugin : IASF, IBot, IBotCommand, IBotConnection, IBotFriendRequest, IBotMessage, IBotModules, IBotTradeOffer {
// This is used for identification purposes, typically you want to use a friendly name of your plugin here, such as the name of your main class
// Please note that this property can have direct dependencies only on structures that were initialized by the constructor, as it's possible to be called before OnLoaded() takes place
public string Name => nameof(ExamplePlugin);
@ -105,6 +105,12 @@ namespace ArchiSteamFarm.CustomPlugins.ExamplePlugin {
// Still, you should take anything other than EResult.OK with a grain of salt, unless you want to assume that Steam knows why it disconnected us (hehe, you bet)
public void OnBotDisconnected(Bot bot, EResult reason) { }
// This method is called when bot receives a friend request or group invite that ASF isn't willing to accept
// It allows you to generate a response whether ASF should accept it (true) or proceed like usual (false)
// If you wanted to do extra filtering (e.g. friend requests only), you can interpret the steamID as SteamID (SteamKit2 type) and then operate on AccountType
// As an example, we'll run a trade bot that is open to all friend/group invites, therefore we'll accept all of them here
public Task<bool> OnBotFriendRequest(Bot bot, ulong steamID) => Task.FromResult(true);
// This method is called at the end of Bot's constructor
// You can initialize all your per-bot structures here
// In general you should do that only when you have a particular need of custom modules or alike, since ASF's plugin system will always provide bot to you as a function argument

View file

@ -1877,6 +1877,14 @@ namespace ArchiSteamFarm {
break;
case EAccountType.Clan:
bool acceptGroupRequest = await Core.OnBotFriendRequest(this, friend.SteamID).ConfigureAwait(false);
if (acceptGroupRequest) {
ArchiHandler.AcknowledgeClanInvite(friend.SteamID, true);
await JoinMasterChatGroupID().ConfigureAwait(false);
break;
}
if (BotConfig.BotBehaviour.HasFlag(BotConfig.EBotBehaviour.RejectInvalidGroupInvites)) {
ArchiHandler.AcknowledgeClanInvite(friend.SteamID, false);
@ -1887,7 +1895,19 @@ namespace ArchiSteamFarm {
if (HasPermission(friend.SteamID, BotConfig.EPermission.FamilySharing)) {
await ArchiHandler.AddFriend(friend.SteamID).ConfigureAwait(false);
} else if (BotConfig.BotBehaviour.HasFlag(BotConfig.EBotBehaviour.RejectInvalidFriendInvites)) {
break;
}
bool acceptFriendRequest = await Core.OnBotFriendRequest(this, friend.SteamID).ConfigureAwait(false);
if (acceptFriendRequest) {
await ArchiHandler.AddFriend(friend.SteamID).ConfigureAwait(false);
break;
}
if (BotConfig.BotBehaviour.HasFlag(BotConfig.EBotBehaviour.RejectInvalidFriendInvites)) {
await ArchiHandler.RemoveFriend(friend.SteamID).ConfigureAwait(false);
}

View file

@ -248,6 +248,30 @@ namespace ArchiSteamFarm.Plugins {
}
}
internal static async Task<bool> OnBotFriendRequest(Bot bot, ulong steamID) {
if ((bot == null) || (steamID == 0)) {
ASF.ArchiLogger.LogNullError(nameof(bot) + " || " + nameof(steamID));
return false;
}
if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) {
return false;
}
IList<bool> responses;
try {
responses = await Utilities.InParallel(ActivePlugins.OfType<IBotFriendRequest>().Select(plugin => plugin.OnBotFriendRequest(bot, steamID))).ConfigureAwait(false);
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
return false;
}
return responses.Any(response => response);
}
internal static async Task OnBotInit(Bot bot) {
if (bot == null) {
ASF.ArchiLogger.LogNullError(nameof(bot));

View file

@ -0,0 +1,37 @@
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// |
// Copyright 2015-2019 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Threading.Tasks;
using JetBrains.Annotations;
namespace ArchiSteamFarm.Plugins {
[PublicAPI]
public interface IBotFriendRequest : IPlugin {
/// <summary>
/// ASF will call this method for unhandled (ignored and rejected) friend requests and Steam group invites received by the bot.
/// </summary>
/// <param name="bot">Bot object related to this callback.</param>
/// <param name="steamID">64-bit Steam identificator of the user that sent a friend request, or a group that the bot has been invited to.</param>
/// <returns>True if the request should be accepted as part of this plugin, false otherwise.</returns>
[NotNull]
Task<bool> OnBotFriendRequest([NotNull] Bot bot, ulong steamID);
}
}