Thread: Discord Bot Base - Any Revision, Any Base.

Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1 Discord Bot Base - Any Revision, Any Base. 
    Extreme Donator

    Patrity's Avatar
    Join Date
    Jul 2008
    Age
    26
    Posts
    759
    Thanks given
    51
    Thanks received
    123
    Rep Power
    126
    This bot uses the JavaCord library. I have used JavaCord and Discord4J and much prefer JavaCord simply because it is more object oriented and queues messages for you, not to send too many messages and potentially have discord ban your bot.

    All documentation regarding Javacord can be found here: [Only registered and activated users can see links. ]

    You will need to include this dependency:

    Gradle:
    Code:
    repositories { mavenCentral() }
    dependencies { implementation 'org.javacord:javacord:3.0.4' }
    Maven:
    Code:
    <dependency>
        <groupId>org.javacord</groupId>
        <artifactId>javacord</artifactId>
        <version>3.0.4</version>
        <type>pom</type>
    </dependency>
    Create a new class:
    Spoiler for Code:
    Code:
    import java.awt.Color;
    import java.util.List;
    import java.util.Optional;
    
    import org.javacord.api.DiscordApi;
    import org.javacord.api.DiscordApiBuilder;
    import org.javacord.api.entity.channel.TextChannel;
    import org.javacord.api.entity.message.MessageBuilder;
    import org.javacord.api.util.logging.ExceptionLogger;
    
    
    /**
     * @author Patrity || https://www.rune-server.ee/members/patrity/
     */
    
    public class JavaCord {
    
    	private static String token = "INSERT DISCORD BOT TOKEN HERE";
    
    	private static String serverName = "YOUR SERVER NAME";
    	
    	private static DiscordApi api = null;
    
    	public static void init() {
    		new DiscordApiBuilder().setToken(token).login().thenAccept(api -> {
    			JavaCord.api = api;
    			System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
    			sendMessage("general", serverName+" is now online!");
    			api.addMessageCreateListener(event -> {
    				
    				if (event.getMessageContent().equalsIgnoreCase("::players")) {
    					int online = (int)PlayerHandler.getRealPlayerCount();
    					event.getChannel().sendMessage("Players currently online: "+online);
    				}				
    				
    				if (event.getMessageContent().equalsIgnoreCase("::online")) {
    					event.getChannel().sendMessage(":tada: "+serverName+" is Online! :tada:");
    				}
    				
    				
    				if (event.getMessageContent().startsWith("::movehome")) {
    					if (event.getMessageAuthor().isServerAdmin()) {
    						System.out.println("perms");
    						String target = event.getMessageContent().replace("::movehome ", "");
    						Optional<Player> optionalPlayer = PlayerHandler.getOptionalPlayer(target);
    						if (optionalPlayer.isPresent()) {
    							Player c2 = optionalPlayer.get();
    							c2.setX(3357);
    							c2.setY(9313);
    							c2.setHeight(0);
    							c2.setNeedsPlacement(true);
    							event.getChannel().sendMessage("You have teleported " + c2.playerName + " to home.");
    							c2.sendMessage("You have been teleported home by " + event.getMessageAuthor().getDisplayName() + ".");
    						} else { 
    							event.getChannel().sendMessage("Player is not online!");
    						}
    					} else {
    						event.getChannel().sendMessage("You do not have permission to preform this command");
    					}
    				}		
    				
    				if (event.getMessageContent().startsWith("::kick")) {
    					if (event.getMessageAuthor().isServerAdmin()) {
    						String target = event.getMessageContent().replace("::kick ", "");
    						Optional<Player> optionalPlayer = PlayerHandler.getOptionalPlayer(target);
    						if (optionalPlayer.isPresent()) {
    							Player c2 = optionalPlayer.get();
    							if (World.getWorld().getMultiplayerSessionListener().inAnySession(c2)) {
    								event.getChannel().sendMessage("The player is in a trade, or duel. You cannot do this at this time.");
    								return;
    							}
    							c2.outStream.writePacketHeader(109);
    							CycleEventHandler.getSingleton().stopEvents(c2);
    							c2.properLogout = true;			
    							c2.disconnected = true;
    							c2.logoutDelay = Long.MAX_VALUE;
    							event.getChannel().sendMessage("Successfully kicked: " + c2.playerName + ".");
    							ConnectedFrom.addConnectedFrom(c2, c2.connectedFrom);
    						} else { 
    							event.getChannel().sendMessage("Player is not online!");
    						}
    					} else {
    						event.getChannel().sendMessage("You do not have permission to preform this command");
    					}
    				}
    				
    			});
    		})
    				// Log any exceptions that happened
    				.exceptionally(ExceptionLogger.get());
    	}
    
    	public static void sendMessage(String channel, String msg) {
    		if (World.localWorld) {
    			return;
    		}
    		try {
    			new MessageBuilder()
    			.append(msg)
    			.send((TextChannel) api.getTextChannelsByNameIgnoreCase(channel).toArray()[0]);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}	
    
    }


    With this class now in your server, you will need to initialize the class to run on server start, that can be done with:
    Code:
    JavaCord.init();
    So, looking back at the JavaCord class you created, you will need to make some changes depending on your base. The commands can use the same means that your in game commands use, for example the kick command. The code in the above kick command can be replaced with the code from what your server currently uses. The potential is really endless.
    Please be careful with these commands and be sure to check for discord admin where required.
    There is also other checks that JavaCord supports such as by Discord role, and Discord user ID. Please refer to the github page.

    Now let's set up our Bot.
    Follow this link: [Only registered and activated users can see links. ]
    Set up a new application

    Give your bot a name and a profile picture (these only appear in your developer window)


    Head to the Bot tab of the application settings
    Name your bot and give it a picture (This is what will appear in your server when the bot speaks)
    Reveal the token on this page and copy it.


    Paste this token in your JavaCord class we created earlier and run your server.

    After the bot initializes it will print out a URL in your server console. Navigate to that url to invite the bot to your discord server.
    You're done!


    Adding Triggered Messages Throughout Your Server:
    There is a method created in this class to easily send messages on events from within the server, whether you want to send logs, drops, events, kills or whatever.
    simply call:
    Code:
    JavaCord.sendMessage("channel", "msg")
    For example when a rare drop is announced in game, you can add this call immediately after the in-game announcement to also announce rare drops in discord.



    Some Tips:
    1. Do not give the bot administration or any other permissions of the sort. If you want to use a bot for user management or other features, use Dyno or MEE6. With this client token being in your source, if your source gets leaked so does the discord token. This can lead to your entire discord server being griefed or deleted.
    2. When adding new commands, be sure that it is checking for the proper discord permissions, such as for kick or ban commands if you plan on adding those to this bot.
    3. READ THE JAVACORD API AND JOIN THEIR DISCORD. There is many more capabilities with this bot such as creating embeds. The community is helpful, but will not spoon-feed you.



    Good luck!
    Free map releases: [Only registered and activated users can see links. ]


    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    ✥Blade Owner✥

    TeJay's Avatar
    Join Date
    Jul 2017
    Posts
    520
    Thanks given
    134
    Thanks received
    184
    Rep Power
    842
    Great job Pat!
    [Only registered and activated users can see links. ]

    Spoiler for Services:
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  4. #3  
    Extreme Donator

    Patrity's Avatar
    Join Date
    Jul 2008
    Age
    26
    Posts
    759
    Thanks given
    51
    Thanks received
    123
    Rep Power
    126
    Quote Originally Posted by TeJay View Post
    Great job Pat!
    Thank ya, buddy!
    Free map releases: [Only registered and activated users can see links. ]


    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  5. #4  
    OWNER @ BOFA (Now Hiring)
    Mr Bill's Avatar
    Join Date
    May 2010
    Posts
    518
    Thanks given
    15
    Thanks received
    31
    Rep Power
    37
    What a legend, thanks
    Reply With Quote  
     

  6. #5  
    Extreme Donator

    Patrity's Avatar
    Join Date
    Jul 2008
    Age
    26
    Posts
    759
    Thanks given
    51
    Thanks received
    123
    Rep Power
    126
    Quote Originally Posted by Mr Bill View Post
    What a legend, thanks
    No problem, bud.
    Free map releases: [Only registered and activated users can see links. ]


    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  7. #6  
    Registered Member Kairon's Avatar
    Join Date
    Feb 2017
    Posts
    201
    Thanks given
    52
    Thanks received
    43
    Rep Power
    33
    Great job!
    Code:
    Reply With Quote  
     

  8. #7  
    Registered Member

    Join Date
    Oct 2017
    Age
    19
    Posts
    218
    Thanks given
    205
    Thanks received
    111
    Rep Power
    286
    cool release, I'm sure a lot of people will find this useful, however I do have a few suggestions/improvements.

    handling the commands like that isn't effective nor clean imo.
    using abstraction would make it a lot easier.


    Code:
    public class DiscordCommands {
    
        public static Map<String, MessageCreateListener> COMMANDS = new HashMap<>();
        public static Map<String, MessageCreateListener> FINAL_COMMAND = new HashMap<>();
    
        public static int execute() {
    
            COMMANDS.putIfAbsent("online", new DiscordOnlineCommand());
            COMMANDS.putIfAbsent("commands", new DiscordListCommand());
            COMMANDS.putIfAbsent("help", new DiscordHelpCommand());
            COMMANDS.putIfAbsent("links", new DiscordHelpfulLinksCommand());
            COMMANDS.putIfAbsent("players", new DiscordOnlineCommand());
            COMMANDS.putIfAbsent("retard", new DiscordSuicRetardness());
    
    
            COMMANDS.forEach((com, exe) -> {
                FINAL_COMMAND.putIfAbsent(DiscordConstant.PREFIX + com, exe);
            });
    
            return FINAL_COMMAND.size();
        }
    }

    and then calling upon the commands upon startup (Whenever/whereever you're initialising the discord bot, just slap that baby underneath)

    Code:
            api.addMessageCreateListener(event -> {
                DiscordCommands.FINAL_COMMAND.forEach((command, execution) -> {
                    if(command.equalsIgnoreCase(event.getMessage().getContent())) {
                        execution.onMessageCreate(event);
                    }
                });
            });
    also Javacord allows you to implement MessageCreateListener, which could be done on each command.

    Code:
    public class DiscordOnlineCommand implements MessageCreateListener {
    
        @Override
        public void onMessageCreate(MessageCreateEvent messageCreateEvent) {
            EmbedBuilder embedBuilder = new EmbedBuilder().
                    setTitle("Players online").
                    addField("Current players online: ", String.valueOf(World.getPlayers().size()), true);
            messageCreateEvent.getChannel().sendMessage(embedBuilder);
        }
    }
    Additionally; could improve on the sendMessage too.

    Code:
    	
    public DiscordManager(String channel, String title, String message) {
    		this.channel = channel;
    		this.title = title;
    		this.message = message;
    	}
    
    	public void log() {
    		EmbedBuilder embedBuilder = new EmbedBuilder().setTitle(title).setColor(Color.green)
    				.addField(player.getUsername() + "", message, true);
    		Discord.api.getChannelById(channel).get().asTextChannel().get().sendMessage(embedBuilder);
    	}
    Overall cool release. good job.
    Spoiler for signature too large:
    end me
    [Only registered and activated users can see links. ]

    Reply With Quote  
     

  9. Thankful user:


  10. #8  
    Extreme Donator

    Patrity's Avatar
    Join Date
    Jul 2008
    Age
    26
    Posts
    759
    Thanks given
    51
    Thanks received
    123
    Rep Power
    126
    Quote Originally Posted by Bitshifting View Post
    cool release, I'm sure a lot of people will find this useful, however I do have a few suggestions/improvements.

    handling the commands like that isn't effective nor clean imo.
    using abstraction would make it a lot easier.


    Overall cool release. good job.

    Thanks buddy, I'll rewrite this tonight and modify my OP.
    Free map releases: [Only registered and activated users can see links. ]


    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  11. #9  
    Registered Member
    Join Date
    Aug 2019
    Posts
    1
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    Thanks for the guide.
    Reply With Quote  
     

  12. #10  
    Registered Member rabea's Avatar
    Join Date
    Mar 2018
    Posts
    341
    Thanks given
    19
    Thanks received
    48
    Rep Power
    69
    Thank you
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [718] [Any Revision]AIO Discord Bot
    By Cody_ in forum Projects
    Replies: 7
    Last Post: 06-08-2019, 07:07 AM
  2. Bob's RSPS Services and Content - Any Revision/Base
    By bobismyname in forum Selling
    Replies: 20
    Last Post: 05-19-2017, 02:03 PM
  3. Any Decent Pk Bases?
    By GreekO in forum Requests
    Replies: 0
    Last Post: 08-02-2013, 01:56 AM
  4. Replies: 2
    Last Post: 03-11-2013, 03:47 AM
  5. Any custom minigame base [INCLUDES USE OF TIMERS]
    By Polaroid in forum Tutorials
    Replies: 24
    Last Post: 10-18-2010, 02:24 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •