События не получают повышение - PullRequest
0 голосов
/ 25 июня 2018

Я создаю BOT Discord и хочу создать еще несколько определенных событий, например, когда возникает событие 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 голосов
/ 25 июня 2018

Проблема была со статическими нестатическими полями.

Правильный код

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 !

...