コード例 #1
0
        private async Task OnMessageReceived(object?sender, TextMessage textMessage)
        {
            if (textMessage?.Message == null)
            {
                Log.Warn("Invalid TextMessage: {@textMessage}", textMessage);
                return;
            }
            Log.Debug("TextMessage: {@textMessage}", textMessage);

            if (!localization.LanguageLoaded)
            {
                var langResult = await localization.LoadLanguage(config.Language, false);

                if (!langResult.Ok)
                {
                    Log.Error("Failed to load language file ({0})", langResult.Error);
                }
            }
            localization.ApplyLanguage();

            textMessage.Message = textMessage.Message.TrimStart(' ');
            if (!textMessage.Message.StartsWith("!", StringComparison.Ordinal))
            {
                return;
            }

            Log.Info("User {0} requested: {1}", textMessage.InvokerName, textMessage.Message);

            ts3client.InvalidateClientBuffer();

            ChannelId?     channelId    = null;
            ClientDbId?    databaseId   = null;
            ChannelGroupId?channelGroup = null;

            ServerGroupId[]? serverGroups = null;

            if (ts3FullClient.Book.Clients.TryGetValue(textMessage.InvokerId, out var bookClient))
            {
                channelId    = bookClient.Channel;
                databaseId   = bookClient.DatabaseId;
                serverGroups = bookClient.ServerGroups.ToArray();
                channelGroup = bookClient.ChannelGroup;
            }
            else if ((await ts3FullClient.ClientInfo(textMessage.InvokerId)).Get(out var infoClient, out var infoClientError))
            {
                channelId    = infoClient.ChannelId;
                databaseId   = infoClient.DatabaseId;
                serverGroups = infoClient.ServerGroups;
                channelGroup = infoClient.ChannelGroup;
            }
            else
            {
                try
                {
                    var cachedClient = await ts3client.GetCachedClientById(textMessage.InvokerId);

                    channelId    = cachedClient.ChannelId;
                    databaseId   = cachedClient.DatabaseId;
                    channelGroup = cachedClient.ChannelGroup;
                }
                catch (AudioBotException cachedClientError)
                {
                    Log.Warn(
                        "The bot is missing teamspeak permissions to view the communicating client. " +
                        "Some commands or permission checks might not work " +
                        "(clientlist:{0}, clientinfo:{1}).",
                        cachedClientError.Message, infoClientError.ErrorFormat());
                }
            }

            var invoker = new ClientCall(textMessage.InvokerUid ?? Uid.Anonymous, textMessage.Message,
                                         clientId: textMessage.InvokerId,
                                         visibiliy: textMessage.Target,
                                         nickName: textMessage.InvokerName,
                                         channelId: channelId,
                                         databaseId: databaseId,
                                         serverGroups: serverGroups,
                                         channelGroup: channelGroup);

            var session = sessionManager.GetOrCreateSession(textMessage.InvokerId);
            var info    = CreateExecInfo(invoker, session);

            // check if the user has an open request
            if (session.ResponseProcessor != null)
            {
                await TryCatchCommand(info, answer : true, async() =>
                {
                    var msg = await session.ResponseProcessor(textMessage.Message);
                    if (!string.IsNullOrEmpty(msg))
                    {
                        await info.Write(msg).CatchToLog(Log);
                    }
                });

                session.ClearResponse();
                return;
            }

            await CallScript(info, textMessage.Message, answer : true, false);
        }