事件没有得到提升

Events doesn't get raise

我正在创建一个 Discord BOT,我想创建一些更具体的事件,例如,当引发 DiscordSocketClient.UserVoiceStateUpdated 事件时,它会调用一个方法来确定用户发生了什么变化(例如加入或离开语音通道)并引发另一个事件。问题是,引发了 none 个事件。

代码

public static class Program
{
    static void Main(string[] args)
    {
        User _user = new User();
        _client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Verbose });
        //event subscriptions
        _client.UserVoiceStateUpdated += User.VoiceStateUpdated;
        _user.Joined += UserAccountManager.User_Joined;
        _user.Left += UserAccountManager.User_Left;
    }
}
public class User
{
    public delegate void VoiceStateChangeEventHandler(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState);
    public event VoiceStateChangeEventHandler Joined;
    public event VoiceStateChangeEventHandler Left;

    //User voice state changes: joined | left | moved
    public static Task VoiceStateUpdated(SocketUser user, SocketVoiceState oldState, SocketVoiceState newState)
    { //<- I inserted a breakpoint here. The execution doesn't get here
        //Joined
        if(oldState.VoiceChannel == null && newState.VoiceChannel != null)
        {
            new User().RaiseJoinedEvent(user, oldState, newState);
        }
        //Left
        if (oldState.VoiceChannel != null && newState.VoiceChannel == null)
        {
            new User().RaiseLeftEvent(user, oldState, newState);
        }
        //Moved (Joined + Left)
        if(oldState.VoiceChannel != null && newState.VoiceChannel != null && oldState.VoiceChannel != newState.VoiceChannel)
        {
            new User().RaiseJoinedEvent(user, oldState, newState);
            new User().RaiseLeftEvent(user, oldState, newState);
        }
        return Task.CompletedTask;
    }

    protected virtual void RaiseJoinedEvent(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState)
    {
        Joined?.Invoke(socketUser, oldSocketVoiceState, newSocketVoiceState); //<- I inserted a breakpoint here. The execution doesn't get here. I played around with the code and when the execution get here the `Joined` has null value.
    }
    protected virtual void RaiseLeftEvent(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState)
    {
        Left?.Invoke(socketUser, oldSocketVoiceState, newSocketVoiceState); //<- I inserted a breakpoint here. The execution doesn't get here. I played around with the code and when the execution get here the `Left` has null value.
    }
}
public static class UserAccountManager
{
    public static void User_Joined(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState)
    { //<- I inserted a breakpoint here. The execution never get here.
        //CODE...
    }

    public static void User_Left(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState)
    { //<- I inserted a breakpoint here. The execution never get here.
        //CODE...
    }
}

我已经尽可能地简化了代码。

有什么建议吗?

问题出在静态非静态字段上。

正确的代码

public static class Program
{
    static void Main(string[] args)
    {
        _client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Verbose });
        //event subscriptions
        _client.UserVoiceStateUpdated += User.VoiceStateUpdated;
        User.Joined += UserAccountManager.User_Joined;
        User.Left += UserAccountManager.User_Left;
    }
}
public class User
{
    public delegate void VoiceStateChangeEventHandler(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState);
    public static event VoiceStateChangeEventHandler Joined;
    public static event VoiceStateChangeEventHandler Left;

    //User voice state changes: joined | left | moved
    public static Task VoiceStateUpdated(SocketUser user, SocketVoiceState oldState, SocketVoiceState newState)
    {
        //Joined
        if(oldState.VoiceChannel == null && newState.VoiceChannel != null)
        {
            RaiseJoinedEvent(user, oldState, newState);
        }
        //Left
        if (oldState.VoiceChannel != null && newState.VoiceChannel == null)
        {
            RaiseLeftEvent(user, oldState, newState);
        }
        //Moved (Joined + Left)
        if(oldState.VoiceChannel != null && newState.VoiceChannel != null && oldState.VoiceChannel != newState.VoiceChannel)
        {
            RaiseJoinedEvent(user, oldState, newState);
            RaiseLeftEvent(user, oldState, newState);
        }
        return Task.CompletedTask;
    }

    protected static void RaiseJoinedEvent(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState)
    {
        Joined?.Invoke(socketUser, oldSocketVoiceState, newSocketVoiceState);
    }
    protected static void RaiseLeftEvent(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState)
    {
        Left?.Invoke(socketUser, oldSocketVoiceState, newSocketVoiceState);
    }
}
public static class UserAccountManager
{
    public static void User_Joined(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState)
    {
        //CODE...
    }

    public static void User_Left(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState)
    {
        //CODE...
    }
}

谢谢你ps2goat