当有人加入或离开语音通道时,使机器人将消息发送到文本通道

时间:2018-11-03 17:07:34

标签: discord.js

我想让我的机器人将消息发送到一个文本通道,该文本通道的输入者是刚进入语音通道或刚离开语音通道的人。我是编码的新手,但是我已经尝试过了,但是没有用。

bot.on('voiceStateUpdate', (oldMember, newMember) => {
  let newUserChannel = newMember.voiceChannel;
  let oldUserChannel = oldMember.voiceChannel;
  if (oldUserChannel === undefined && newUserChannel !== undefined) {
    if (newUserChannel === bot.voiceChannel) {
      console.log("Hello");
    }
  } else if (newUserChannel === undefined) {
  }
});

控制台上什至没有任何显示。

1 个答案:

答案 0 :(得分:0)

我认为这不起作用,因为您正在使用using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Threading.Tasks; using DC6Rounds.Pages; using Windows.ApplicationModel.Core; using Windows.UI; using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Microsoft.Identity.Client; using System.Linq; using Windows.UI.Popups; namespace DC6Rounds { public sealed partial class MainPage : Page { public static object root; public static string rooturl = "https://graph.microsoft.com/v1.0/sites/xxxx.sharepoint.com,Some-identifier,some-identifier/"; public string mezzanineurl = rooturl + "lists/426d0dc6-1aa1-45f4-8a5b-0d13dcf918d0/items"; public string otherurl = rooturl + "lists/294cab78-38c4-4d01-adaa-9194d4fbd049/items"; public MainPage() { this.InitializeComponent(); } private async void SubmitButton_Click(object sender, RoutedEventArgs e) { App.SerialNumber = App.Date + "-" + App.Shift; var (authResult, message) = await Authentication.AquireTokenAsync(); if (authResult != null) { await SubmitDataWithTokenAsync(mezzanineurl, authResult.AccessToken); await SubmitDataWithTokenAsync(otherurl, authResult.AccessToken); } MainSubmitButton.Visibility = Visibility.Collapsed; FrameContent.Navigate(typeof(GeneralPage)); ResultText.Text = "Data Submitted to SharePoint!"; } public async Task<string> SubmitDataWithTokenAsync(string url, string token) { object root; //ADDED THIS LINE TO MAKE THE CODE WORK. var httpClient = new HttpClient(); HttpResponseMessage response; try { if (url == mezzanineurl) { var root = new { fields = new Dictionary<string, string> { { "SerialNumber", App.SerialNumber }, { "PDUAlarms", App.PDUAlarms }, { "DCPA136Alarms", App.DCPA136Alarms }, { "DCPA146Alarms", App.DCPA146Alarms }, { "DCPC137Alarms", App.DCPC137Alarms }, { "DCPC147Alarms", App.DCPC147Alarms }, { "DCPE137Alarms", App.DCPE137Alarms }, { "DCPE147Alarms", App.DCPE147Alarms }, { "DCPA136Amps", App.DCPA136Amps }, { "DCPA146Amps", App.DCPA146Amps }, { "DCPC137Amps", App.DCPC137Amps }, { "DCPC147Amps", App.DCPC147Amps }, { "DCPE137Amps", App.DCPE137Amps }, { "DCPE147Amps", App.DCPE147Amps }, { "Vesda1Alarms", App.Vesda1Alarms }, { "Vesda9Alarms", App.Vesda9Alarms }, { "Vesda2Alarms", App.Vesda2Alarms }, { "Vesda10Alarms", App.Vesda10Alarms }, { "Vesda3Alarms", App.Vesda3Alarms }, { "Vesda11Alarms", App.Vesda11Alarms }, { "Vesda4Alarms", App.Vesda4Alarms }, { "Vesda12Alarms", App.Vesda12Alarms }, { "Vesda5Alarms", App.Vesda5Alarms }, { "Vesda13Alarms", App.Vesda13Alarms }, { "Vesda6Alarms", App.Vesda6Alarms }, { "Vesda14Alarms", App.Vesda14Alarms }, { "Vesda7Alarms", App.Vesda7Alarms }, { "Vesda15Alarms", App.Vesda15Alarms }, { "Vesda8Alarms", App.Vesda8Alarms }, { "Vesda16Alarms", App.Vesda16Alarms }, } }; } else { var root = new { fields = new Dictionary<string, string> { { "SerialNumber", App.SerialNumber }, { "Notes", App.Notes }, { "Z1P1Leaks", App.Z1P1Leaks }, { "Z1D1Leaks", App.Z1D1Leaks }, { "Z1P5Leaks", App.Z1P5Leaks }, { "Z1P3Leaks", App.Z1P3Leaks }, { "Z1P4Leaks", App.Z1P4Leaks }, { "Z3P2Leaks", App.Z3P2Leaks }, { "AirCompressorLeaks", App.AirCompressorLeaks }, { "Z3P4Leaks", App.Z3P4Leaks }, { "Z3P1Leaks", App.Z3P1Leaks }, { "Z3P3Leaks", App.Z3P3Leaks }, { "Z2P1Leaks", App.Z2P1Leaks }, { "Z1P2Leaks", App.Z1P2Leaks }, { "PreactionLog", App.PreactionLog }, { "FirePanelAlarms", App.FirePanelAlarms }, { "Car1Inspection", App.Car1Inspection }, { "Car1Operational", App.Car1Operational }, { "Car2Inspection", App.Car2Inspection }, { "Car2Operational", App.Car2Operational }, { "Date", App.Date }, { "Day", App.Day }, { "Shift", App.Shift }, { "Engineer", App.Engineer }, { "TimeStarted", App.TimeStarted }, { "EquipmentSummary", App.EquipmentSummary }, { "BMSAlarms", App.BMSAlarms }, { "OAT", App.OAT }, } }; } var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }; var content = JsonConvert.SerializeObject(root, s); var request = new HttpRequestMessage(HttpMethod.Post, url); //Add the token in Authorization header request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); request.Content = new StringContent(content, Encoding.UTF8, "application/json"); response = await httpClient.SendAsync(request); var responseString = await response.Content.ReadAsStringAsync(); return responseString; } catch (Exception ex) { return ex.ToString(); } } } } ,但是它不存在:using Newtonsoft.Json; using System.Collections.Generic; namespace DC6Rounds { class MezzanineListItems { public class Lookup { public string SerialNumber { get; set; } public string id { get; set; } public override string ToString() { return SerialNumber; } } public class Value { public Lookup fields { get; set; } } public class Fields { [JsonProperty("@odata.etag")] public string ODdataETag { get; set; } public string PDUAlarms { get; set; } public string DCPA136Alarms { get; set; } public string DCPA146Alarms { get; set; } public string DCPC137Alarms { get; set; } public string DCPC147Alarms { get; set; } public string DCPE137Alarms { get; set; } public string DCPE147Alarms { get; set; } public string DCPA136Amps { get; set; } public string DCPA146Amps { get; set; } public string DCPC137Amps { get; set; } public string DCPC147Amps { get; set; } public string DCPE137Amps { get; set; } public string DCPE147Amps { get; set; } public string Vesda1Alarms { get; set; } public string Vesda9Alarms { get; set; } public string Vesda2Alarms { get; set; } public string Vesda10Alarms { get; set; } public string Vesda3Alarms { get; set; } public string Vesda11Alarms { get; set; } public string Vesda4Alarms { get; set; } public string Vesda12Alarms { get; set; } public string Vesda5Alarms { get; set; } public string Vesda13Alarms { get; set; } public string Vesda6Alarms { get; set; } public string Vesda14Alarms { get; set; } public string Vesda7Alarms { get; set; } public string Vesda15Alarms { get; set; } public string Vesda8Alarms { get; set; } public string Vesda16Alarms { get; set; } } public class RootObject { [JsonProperty("@odata.context")] public string ODataContext { get; set; } [JsonProperty("@odata.etag")] public string ODdataETag { get; set; } [JsonProperty("fields@odata.context")] public string FieldsODataContext { get; set; } public Fields fields { get; set; } public List<Value> value { get; set; } } } } GuildMember的属性,因此您需要获取该成员首先。
我会这样:

using Newtonsoft.Json;
using System.Collections.Generic;

namespace DC6Rounds
{
    class OtherListItems
    {
        public class Lookup
        {
            public string SerialNumber { get; set; }
            public string id { get; set; }
            public override string ToString()
            {
                return SerialNumber;
            }
        }

        public class Value
        {
            public Lookup fields { get; set; }
        }

        public class Fields
        {
            [JsonProperty("@odata.etag")]
            public string ODdataETag { get; set; }

            public string Notes { get; set; }
            public string Z1P1Leaks { get; set; }
            public string Z1D1Leaks { get; set; }
            public string Z1P5Leaks { get; set; }
            public string Z1P3Leaks { get; set; }
            public string Z1P4Leaks { get; set; }
            public string Z3P2Leaks { get; set; }
            public string AirCompressorLeaks { get; set; }
            public string Z3P4Leaks { get; set; }
            public string Z3P1Leaks { get; set; }
            public string Z3P3Leaks { get; set; }
            public string Z2P1Leaks { get; set; }
            public string Z1P2Leaks { get; set; }
            public string PreactionLog { get; set; }
            public string FirePanelAlarms { get; set; }
            public string Car1Inspection { get; set; }
            public string Car1Operational { get; set; }
            public string Car2Inspection { get; set; }
            public string Car2Operational { get; set; }
            public string Date { get; set; }
            public string Day { get; set; }
            public string Shift { get; set; }
            public string Engineer { get; set; }
            public string TimeStarted { get; set; }
            public string EquipmentSummary { get; set; }
            public string BMSAlarms { get; set; }
            public string OAT { get; set; }
        }

        public class RootObject
        {
            [JsonProperty("@odata.context")]
            public string ODataContext { get; set; }

            [JsonProperty("@odata.etag")]
            public string ODdataETag { get; set; }

            [JsonProperty("fields@odata.context")]
            public string FieldsODataContext { get; set; }

            public Fields fields { get; set; }

            public List<Value> value { get; set; }
        }
    }
}