Skip to content

Commit

Permalink
http://pastebin.com/8dLnnFav + fixed examples for new listener data
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Feb 5, 2016
1 parent 4cc0cc3 commit 228d845
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 42 deletions.
24 changes: 12 additions & 12 deletions examples/ChannelListener.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import ml.hlaaftana.discordg.objects.*
import ml.hlaaftana.discordg.APIBuilder

API api = APIBuilder.build("example@example.com", "example123")
api.addListener("channel create") { Event e ->
if (e.data.channel instanceof TextChannel && e.data.guild != null){
e.data.channel.sendMessage("Hello there, new channel!")
api.addListener("channel create") { Map d ->
if (d.channel instanceof TextChannel && d.guild != null){
d.channel.sendMessage("Hello there, new channel!")
}
}
api.addListener("channel delete") { Event e ->
if (e.data.guild != null)
e.data.guild.defaultChannel.sendMessage("Looks like $e.data.channel.type channel \"$e.data.channel.name\" was deleted.")
api.addListener("channel delete") { Map d ->
if (d.guild != null)
d.guild.defaultChannel.sendMessage("Looks like $d.channel.type channel \"$d.channel.name\" was deleted.")
}

api.addListener("channel update") { Event e ->
api.addListener("channel update") { Map d ->
Channel oldChan
TextChannel chan
if (e.data.channel instanceof TextChannel){
chan = e.data.channel
oldChan = e.data.guild.getTextChannelById(e.data.channel.id)
if (d.channel instanceof TextChannel){
chan = d.channel
oldChan = d.guild.getTextChannelById(d.channel.id)
chan.sendMessage("It seems this channel was modified.")
}else{
chan = e.data.guild.defaultChannel
oldChan = e.data.guild.getVoiceChannelById(e.data.channel.id)
chan = d.guild.defaultChannel
oldChan = d.guild.getVoiceChannelById(d.channel.id)
chan.sendMessage("It seems voice channel $chan.name was modified.")
}
}
12 changes: 6 additions & 6 deletions examples/MemberListener.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import ml.hlaaftana.discordg.objects.*
import ml.hlaaftana.discordg.APIBuilder

API api = APIBuilder.build("example@example.com", "example123")
api.addListener("guild member add") { Event e ->
e.data.guild.defaultChannel.sendMessage("Welcome to the server, $e.data.member.mention!")
api.addListener("guild member add") { Map d ->
d.guild.defaultChannel.sendMessage("Welcome to the server, $d.member.mention!")
}
api.addListener("guild member remove") { Event e ->
e.data.guild.defaultChannel.sendMessage("Aww, $e.data.member.mention left the server.")
api.addListener("guild member remove") { Map d ->
d.guild.defaultChannel.sendMessage("Aww, $d.member.mention left the server.")
}

api.addListener("guild member update") { Event e ->
e.data.guild.defaultChannel.sendMessage("I see your roles changed, $e.data.member.mention.")
api.addListener("guild member update") { Map d ->
d.guild.defaultChannel.sendMessage("I see your roles changed, $d.member.mention.")
}
12 changes: 6 additions & 6 deletions examples/PingPong.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ API api = APIBuilder.build("example@example.com", "example123")
* https://github.com/hlaaftana/DiscordG/blob/master/src/hlaaftana/discordg/request/WSClient.groovy#L89-L236
* for the data yourself however.
* And, to add to that, every event has the raw JSON data in them to
* which is "e.data["fullData"]" or "e.data.fullData".
* which is "d["fullData"]" or "d.fullData".
* You can read what the raw JSON data for each event is by checking the
* "d" objects in each event here:
* http://hornwitser.no/discord/analysis
*/
api.addListener("message create") { Event e ->
api.addListener("message create") { Map d ->
// MESSAGE_CREATE's data is just one Message object called "message".
// We can get the content of that message with Message#getContent() which Groovy fills in.
if (e.data.message.content.startsWith("!ping")){
// Note that you can replace "e.data.message" with "e.data["message"]".
e.data.sendMessage("Pong!")
if (d.message.content.startsWith("!ping")){
// Note that you can replace "d.message" with "d["message"]".
d.sendMessage("Pong!")
// Simple as that! You could even send the message with TTS by doing:
// e.data.sendMessage("Pong!", true)
// d.sendMessage("Pong!", true)
}
}
// Sidenote, instead of "APIBuilder.build("email", "password")" above, you could have typed
Expand Down
4 changes: 2 additions & 2 deletions examples/PingPongPlus.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ Closure getCommandArgs = { String message, String command ->
}

api = APIBuilder.build("email@example.com", "password123")
api.addListener("message create", { // We didn't type "Event e ->" here, since we can just use
api.addListener("message create", { // We didn't type "Map d ->" here, since we can just use
// an implicit parameter which Groovy names "it".
// Doing this because it'll get annoying to refer to the same variable
// in the same lengthy way later. Don't blame me, you're gonna end up
// having special handling with this API anyway. Kinda the point of it
// being so low-level.
Message message = it.data["message"]
Message message = it["message"]
// Refer to isMessageCommand above to understand how it works.
if (isMessageCommand(message.content, "ping")){
Expand Down
12 changes: 6 additions & 6 deletions examples/ServerListener.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import ml.hlaaftana.discordg.objects.*
import ml.hlaaftana.discordg.APIBuilder

API api = APIBuilder.build("example@example.com", "example123")
api.addListener("server create") { Event e ->
e.data.server.defaultChannel.sendMessage("Hello there, new channel!")
api.addListener("server create") { Map d ->
d.server.defaultChannel.sendMessage("Hello there, new channel!")
}
api.addListener("server delete") { Event e ->
println "It seems I left or was banned in/kicked out of " + e.data.server.name
api.addListener("server delete") { Map d ->
println "It seems I left or was banned in/kicked out of " + d.server.name
}

api.addListener("server update") { Event e ->
e.data.server.defaultChannel.sendMessage("Seems this server updated.")
api.addListener("server update") { Map d ->
d.server.defaultChannel.sendMessage("Seems this server updated.")
}
12 changes: 4 additions & 8 deletions examples/bot/CommandBotExample.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@ bot.defaultPrefix = ["!", "\$"] // can be a string or a list. i know it being a
// can also be a list. do note that after "hello" i can add a list or string as
// a list of prefixes / a prefix
bot.addCommand new Command("hello"){
def run(Event e){
e.data.sendMessage("Hello there, ${e.data.message.author.mention}!")
def run(Map d){
d.sendMessage("Hello there, ${d.message.author.mention}!")
}
}

// we add the response first since prefix values are optional
bot.addCommand new ResponseCommand("Hello there, but sadly I can't access the event data using this method of responding to commands. :'(", "hello")

// again, we add the response closure first since prefix values are optional
bot.addCommand new ClosureCommand({ Event e, Command c ->
// you can have two or one arguments.
// if you have two arguments, make sure to make the first one the event object,
// and the second one a command object.
// if you have one, make sure to make that one an event object.
e.data.sendMessage("Hello there, ${e.data.message.author.mention}!")
bot.addCommand new ClosureCommand({ Map d ->
d.sendMessage("Hello there, ${d.message.author.mention}!")
}, "hello")

// here we login with the api and register our listeners.
Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/ml/hlaaftana/discordg/objects/Member.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Member extends User{
*/
String getGame(){
try{
return this.server.this.object["presences"].find({ it.user.id == this.user.id }).game.name
return this.server.object["presences"].find({ it.user.id == this.user.id }).game.name
}catch (ex){
return null
}
Expand All @@ -70,7 +70,7 @@ class Member extends User{
*/
String getStatus(){
try{
return this.server.this.object["presences"].find({ it.user.id == this.user.id }).status
return this.server.object["presences"].find({ it.user.id == this.user.id }).status
}catch (ex){
return "offline"
}
Expand Down

0 comments on commit 228d845

Please sign in to comment.