lmariscal / twitchirc

Twitch Bot Development made Easier | DEPRECATED
Other
40 stars 15 forks source link

Cannot make static reference to... #12

Closed Squidkingdom closed 7 years ago

Squidkingdom commented 7 years ago

Hello, I am a relatively new Java programmer and had a question about the connect method. The connect function is not static and the Main Method is. I'm getting errors and i don't know what to do.

lmariscal commented 7 years ago

Yeah no problem, the connect method isn't static because you need a bot object to connect, so the bot gets all the info related to the connection.
If you are lost you can always follow the wiki that will guide you to create your first bot.

If any thing else I'm here to help.

lmariscal commented 7 years ago

Annndddd I just read the last part sry :/ I recommend you to build the bot in a separate class. And link everything together then.

Squidkingdom commented 7 years ago

OMG, I had no idea there was a wiki! Sorry!

Squidkingdom commented 7 years ago

Ok, so i got farther than I was, by using the wiki. Then I started to run into problems. " "User" (In the onMessage parameters) cannot be resolved to a type " and the " bot.start(); " is expecting an identifier. I'm really sorry for wasting your time in advanced, and Ill be the first to admit that IDK how this API is built and or used. Thanks sooooo much for the help!

package com.cavariux.twitchirc.Core;
import java.io.IOException;

import com.cavariux.twitchirc.Chat.Channel;

public class Main {

    public static void main(String[] args) {
        RansomBot bot = new RansomBot();
        bot.connect();
        Channel channel = bot.joinChannel("#channel");
        bot.sendMessage("Hi, Im connected!", channel);

        public static void onMessage(User user, Channel channel, String message){

        }

        User.@getUser("squidkingdom")
        bot.start();

}
mvarendorff commented 7 years ago

It looks like you are messing up a bit there :) You basically need two classes: Main.java and RansomBot.java The Main one is basically just there to get your bot rolling and should include:

In that order.

The line User.@getUser("squidkingdom") is redundant (and missing a ; ;) ). It would give you an object of the type user and you do not need that there :)

The RansomBot.java will contain handling anything else, with the most important thing beeing the constructor containing

The part with onMessage will go in there as well (as a seperate function apart from the constructor).

Hope I could help! Good luck with testing :)

Squidkingdom commented 7 years ago

Thank You!

Squidkingdom commented 7 years ago

Im overriding the onMessage function and its not responding in the chat.

package com.cavariux.twitchirc.Core;

import com.cavariux.twitchirc.Core.*;
import com.cavariux.twitchirc.Chat.*;
@SuppressWarnings("unused")

public class RansomBot extends TwitchBot {
    public RansomBot (){
        this.setUsername("Ransombot");
        this.setOauth_Key("*Censored*");
    }
    @Override
    public void onCommand(User user, Channel channel, String message)
    {
        if (message.equalsIgnoreCase("hello"))
            sendMessage("Hello" + user, channel);
    }
}
package com.cavariux.twitchirc.Core;
//import java.io.IOException;

import com.cavariux.twitchirc.Chat.Channel;

public class Main {

    public static void main(String[] args) {
        RansomBot bot = new RansomBot();
        bot.connect();
        Channel channel = bot.joinChannel("EnjoiHunt");
        bot.sendMessage("Hi, Im connected!", channel);  
        bot.start();
}
}
Squidkingdom commented 7 years ago

I'm so sorry for wasting your time, i'm doing this for a friend and my "Deadline" is coming up quick! I really appreciate it BTW

lmariscal commented 7 years ago

onCommand only works when the user inputs a message starting with !
onMessage works with all messages in chat.

Are you receiving the Hi, I'm connect message in chat?

PD: Don't publish your oath key anywhere, I can guarantee you I will not use it neither geisterfurz007 he is a cool dude but we can't be certain of any other person looking at the issues.

Squidkingdom commented 7 years ago

Thanks for the tip, and I'm not getting that message. I see the message in the console but its not showing up in the chat

lmariscal commented 7 years ago

Are you sure the username and the ouathkey are the right ones? It's the first thing that came to my head.

Squidkingdom commented 7 years ago

Lemme check

lmariscal commented 7 years ago

I think I found the error, put all lowercase the channel you wanna join and for styling (not necessary) put a # infront of the channel.

Channel channel = bot.joinChannel("#enjoihunt");

PD: I tested it should all work I literally copy pasted your code just replaced the key and username and change the channel to the one I gave you. For future joins put the channel in lowercase

Squidkingdom commented 7 years ago

Nope it wont respond, i am testing it in a live chat with a live stream, @RansomBot is the bot name and it wont show up in the chat. Edit: The bot doesnt show in the viewer list either

lmariscal commented 7 years ago

Don't worry about the viewers list Twitch irc is not well done when I did this lib I investigated the whole api and how they managed. Not that well made.
About your issue I'm running short on Ideas :/

I know I just told you not to share your key but can you like your code to test it out? Then when I tell you just edit it and remove the ouath key. thanks

Squidkingdom commented 7 years ago
package com.cavariux.twitchirc.Core;

import com.cavariux.twitchirc.Core.*;
import com.cavariux.twitchirc.Chat.*;
@SuppressWarnings("unused")

public class RansomBot extends TwitchBot {
    public RansomBot (){
        this.setUsername("Ransombot");
        this.setOauth_Key("*censored*");
    }
    @Override
    public void onCommand(User user, Channel channel, String message)
    {
        if (message.equalsIgnoreCase("hello"))
            sendMessage("Hello" + user, channel);
    }
}
package com.cavariux.twitchirc.Core;
//import java.io.IOException;

import com.cavariux.twitchirc.Chat.Channel;

public class Main {

    public static void main(String[] args) {
        RansomBot bot = new RansomBot();
        bot.connect();
        Channel channel = bot.joinChannel("enjoiHunt");
        bot.sendMessage("Hi, Im connected!", channel);  
        bot.start();
}
}

//TwitchBot.java

mvarendorff commented 7 years ago

I cannot test it right now, but as cavariux already mentioned, your channelname should be in lowercase. I think the # is redundant after the latest changes of the library, but to be on the safe side, include it (as I do not know which state your library is in). Another thing that might cause the issue might be your username. I do not know how picky the irc chat is, but you should have the username in lowercase as well. And another note (to be on the safe side): The Oauth_Key is NOT your password for the account! Go to this website and login with your bots account there. The website will supply you with a key that (including the oauth:) should be set as key inside the bot.

Squidkingdom commented 7 years ago

one more thing :), Is the ban(); method private, im getting visablity errors

mvarendorff commented 7 years ago

They should not be. Both, in the Channel and in the User class are public... Is the bot joining properly now?

Squidkingdom commented 7 years ago

Yes it is @geisterfurz007, something was weirdly wrong. I sent the source code to cav and it worked for him. I have a function that checks if a user has been warned. edit I also dont know how to instruct the bot on who to ban :P

public boolean checkWarnList (){
        String line = null;
        try {
            FileReader fileReader = 
                new FileReader(fileName);

            BufferedReader bufferedReader = 
                new BufferedReader(fileReader);

           while((line = bufferedReader.readLine()) != null) {
                String ign = null;
                if(line == ign){
                    ban(user);
                }
            }   
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");
        }
        return false; //False is a placeholder
    }
mvarendorff commented 7 years ago
while((line = bufferedReader.readLine()) != null) {
                String ign = null;
                if(line == ign){
                    ban(user);
                }
            }   

That block is weird... The loop will run only as longs as line is not null, then you compare this (ensured !=null) to something that you just initialized with null, which should never be true. Anyways, in which class do you have this block of code? If this is in the RansomBot class you have to specify the channel from which the user is supposed to be banned from. You need both parameters for banning: User and Channel.

Squidkingdom commented 7 years ago

so the syntax is ban(user, channel)

mvarendorff commented 7 years ago

No it is channel.ban(user) or user.ban(channel) you can use both as the one refers to the other :)

Squidkingdom commented 7 years ago
while((line = bufferedReader.readLine()) != null) {
                String ign = null;
                if(line == ign){
                    user.ban(channel);
                }
            } 

It cant find channel as a variable

mvarendorff commented 7 years ago

When is your checkWarnList() method triggered?

Squidkingdom commented 7 years ago

It will he callee when the bot detects unwanted language (Mainly Slurs). I want the bot to give them a warning and then punish them.

mvarendorff commented 7 years ago

So you are reacting on that in the onMessage() method? If that is the case you can pass the user and the channel to the list. You need some way to get the User and the Channel passed to the method. And then using these objects execute the command.

Squidkingdom commented 7 years ago

yes so 'onMessage(User user, String message Channel channel){ if(mesage.contains("censored slur")){ Boolean warned = checkWarnList(user); if(warned = true) { Channel.ban(user); //Tell then they have been banned } else{ // code that will add them to the list }

Squidkingdom commented 7 years ago

P.S. my code may be finicky im currently on mobile

mvarendorff commented 7 years ago

Yeah something like that should work out. Note that you have to refer to the channel object (lowercase) (unsure if related to mobile typing or not) but that should be a working skeleton for that I guess.

Squidkingdom commented 7 years ago

I am using channel as an object and its still giving me visibility errors, does it have to do with where im defining my method (Its in the botty.Java file). here is the error "The field TwitchBot.user is not visible" code. P.S. There is a lot of left over methods the are superfluous.

package no.onecaresaboutpackages.twitchmylife;

import com.cavariux.twitchirc.Chat.Channel;
import com.cavariux.twitchirc.Chat.User;
import com.cavariux.twitchirc.Core.TwitchBot;
import java.io.*;
import java.util.*;
public class Botty extends TwitchBot {
    String fileName = "warn.txt";
    String fileLine = null;

    Botty() {
        this.setUsername("ransombot");
        this.setOauth_Key("oauth:nyz90nezdhre3t8glmbqr0f94f80fr");
    }
    // Slur Censor
    @Override
    public void onMessage(User user, Channel channel, String message){
            if (message.contains("***")){
                //this is where the code to call the function
                //String warned = checkWarnList
                sendMessage("Please do not use slurs in the chat. (Warning) (" + user + ")", channel);      
            }

    }
    //Unused code to check amount of line in the file, might be used instead of weird null thing.
    //public void onCommand(User user, Channel channel, String message){
    //  if (message.equals("greeting")) {
    //      this.sendMessage("Hi there " + user, channel);
    //  } 
    // }
    //public int checkWarnLines() throws IOException{
        //int t = 0;
        //Scanner file = new Scanner(new File(fileName));
        //while(file.hasNextLine()){
            //t++;
             // file.nextLine();
       // }
        //file.close();
        //return t;

    //}

    public boolean checkWarnList(Channel channel){
        String line = null;
        try {
            FileReader fileReader = 
                new FileReader(fileName);

            BufferedReader bufferedReader = 
                new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                String ign = null;
                if(line == ign){
                    user.ban(channel);
                }
            }    
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");
        }
        return false; //placeholder
    }
}
mvarendorff commented 7 years ago

Pass the user object you get from onMessage() as well so your method call looks like this: checkWarnList(channel, user) and modify the methods header so that it will accept the user object as well: public boolean checkWarnList(Channel channel, User user) {

The problem currently occuring is that user is in fact a global variable of the bot itself, which is private. As there is no other variable with the name user in the scope of your method, the compiler thinks you want to make use of this private global one. So passing the user as well should end this problem.

Squidkingdom commented 7 years ago

I hope this is the last thing XD. But how would one convert a User var into a String, i need to compare someones IGN to a line on a file. (to check if previously warned) @geisterfurz007

lmariscal commented 7 years ago

.toString()