活动没有增加

时间:2018-06-25 19:15:35

标签: c# events discord.net

我正在创建一个Discord BOT,并且我想创建一些更多指定的事件,例如,当引发DiscordSocketClient.UserVoiceStateUpdated事件时,它调用一种方法,该方法可以找出用户发生了什么变化(例如,加入声音或发出声音)频道)并引发另一个事件。问题是,没有任何事件引发。

代码

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...
    }
}

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

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

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

正确的代码

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