【问题标题】:Accessing variable from one class from another从一个类访问另一个类的变量
【发布时间】:2020-11-21 15:16:44
【问题描述】:

我想在文件“b”中使用文件“a”中的变量。我在网上搜索了一下,还是不行。

所以我得到了文件bot.cs 和文件profanityFilter.cs。

在profanityFilter.cs 中我想使用来自bot.cs 的incomingMessage。

bot.cs:

class Bot
{
        public static void Client_OnMessageReceived(object sender, OnMessageReceivedArgs e)
        {
            var incomingMessage = e.ChatMessage.Message;

            //Profanity filter testing received message (need to get incoming message over to profanityFilter.cs)

            profanityFilter.Filter();

            Console.WriteLine($"[{TwitchInformation.BotName}]: [{e.ChatMessage.DisplayName}]: 
            {e.ChatMessage.Message}");
        }
} 

profanityFilter.cs:

class profanityFilter
{
        public static async void Filter()
        {
            var client = new HttpClient();

            Bot message = new Bot();
            var incomingMessagesFromUser = message.Client_OnMessageReceived.IncomingMessages;

            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri("https://neutrinoapi-bad-word-filter.p.rapidapi.com/bad-word-filter"),
                Headers =
                {
                    { "x-rapidapi-key", "xxx" },
                    { "x-rapidapi-host", "neutrinoapi-bad-word-filter.p.rapidapi.com" },
                },
                Content = new FormUrlEncodedContent(new Dictionary<string, string>
                {
                    { "censor-character", "*" },
                    { "content", incomingMessagesFromUser },
                }),
            };

            using (var response = await client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();
                var body = await response.Content.ReadAsStringAsync();
                Console.WriteLine(body);
            }
        }
}

但我收到此屏幕截图中显示的错误:

【问题讨论】:

标签: c# variables scope chatbot profanity


【解决方案1】:

只需让profanityFilter.Filter 接受一个参数,即来自机器人的消息,即

public static async void Filter(string message)

    public static async void Filter(string message)
    {
        var client = new HttpClient();

        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri("https://neutrinoapi-bad-word-filter.p.rapidapi.com/bad-word-filter"),
            Headers =
            {
                { "x-rapidapi-key", "xxx" },
                { "x-rapidapi-host", "neutrinoapi-bad-word-filter.p.rapidapi.com" },
            },
            Content = new FormUrlEncodedContent(new Dictionary<string, string>
            {[enter image description here][1]
                { "censor-character", "*" },
                { "content", message },
            }),
        };
        using (var response = await client.SendAsync(request))
        {
            response.EnsureSuccessStatusCode();
            var body = await response.Content.ReadAsStringAsync();
            Console.WriteLine(body);
        }
    }

PS:通常在C#中的类名是Pascal Cased

【讨论】:

  • 谢谢,我会调查的。
  • 查看我的更新答案,更简单。我在事件和事件处理程序方面领先于自己哈哈?
  • 为什么要公开静态异步字符串过滤器(字符串消息)?
  • 好吧,如果您不需要返回响应,那没关系,您不需要它,删除 string 使用 void 并删除 returns。我假设您想将过滤后的消息返回给机器人类,但也许您不想
  • 更新答案以反映这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2020-11-24
  • 1970-01-01
  • 2017-03-22
  • 2014-01-18
  • 2015-01-21
相关资源
最近更新 更多