forked from bragef/pidgey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpidgey.js
More file actions
executable file
·159 lines (125 loc) · 5.46 KB
/
pidgey.js
File metadata and controls
executable file
·159 lines (125 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/env node
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
const strings = require("./strings.json");
const process = require("process");
const fs=require('fs');
const poifinder = require("./poifind.js");
poifinder.load(config.poifile);
// Discord limitation
const MAX_MESSAGE_SIZE = 2000;
// Number of hits to show in guild
const MAX_HITS_CHANNEL = 10;
// Number if hits to show in DM
const MAX_HITS_DM = 25;
var writeLog = function(logfile, message) {
if(config.logdir == null) return;
if(!fs.existsSync(config.logdir)) fs.mkdirSync(config.logdir);
fs.appendFile(config.logdir + "/" + logfile,
(new Date()).toISOString() + "\t" + message + "\n",
function(err) {});
}
client.on("message", async message => {
// Ignore all bots.
if(message.author.bot) return;
// Ignore messages not starting with !
if(message.content.indexOf(config.prefix) !== 0) return;
// Get command and argument if given.
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const isDirectMessage = ( message.guild === null );
if(command===config.command) {
let scope = "";
let clientMessage;
let matches = null, query, showHelp;
// Shop up to 10 matches in chats, 250 hits in dm's
let maxHits = isDirectMessage ? MAX_HITS_DM : MAX_HITS_CHANNEL;
// If no arguments, or single argument pokestop|gym, show help text
if(!args[0])
showHelp = true;
if(!showHelp && poifinder.isPoiType(args[0].toLowerCase())) {
scope = args.shift().toLowerCase();
}
if(!args[0])
showHelp = true;
if(!showHelp) {
// If numeric query, return poi by number. This works
// becase all numbers which are part of poi names are
// are excluded.
query = args.join(" ");
if( /^[0-9]+$/.test(query)) {
matches = poifinder.getByNumber(query);
}
if(!matches || !matches.length)
matches = poifinder.find(query, scope);
}
if(showHelp) {
const embed = new Discord.RichEmbed();
embed.setTitle(config.description)
.setDescription(config.prefix + config.command + strings[config.language]["searchstring"]);
clientMessage = {embed};
} else if(!matches || matches.length == 0) {
clientMessage = strings[config.language]["nomatches"].replace('{term}',query);
} else if(match = poifinder.singleMatch(matches, query, scope)) {
let coord=match.latitude +"%2C"+match.longitude;
let mapurl = 'https://maps.googleapis.com/maps/api/staticmap?size=512x512&zoom=15&scale=2&key=' + config.google_api_key;
mapurl=mapurl + '¢er='+coord;
if(match.poitype=='gym')
mapurl=mapurl+"&markers=color:green%7Clabel:G%7C"+coord;
else if(match.poitype=='portal')
mapurl=mapurl+"&markers=color:blue%7Clabel:P%7C"+coord;
else
mapurl=mapurl+"&markers=color:red%7Clabel:S%7C"+coord;
const embed = new Discord.RichEmbed();
let description = "[OpenStreetMap](http://www.openstreetmap.org/?mlat=" +
match.latitude + "&mlon=" +
match.longitude + "&zoom=15&layers=M)" +
" / " +
"[Google Maps](https://www.google.com/maps/dir/?api=1&dir_action=travelmode=walking&navigate&destination=" +
coord + ")";
if(match.description.length > 0) {
description = match.description + "\n" + description;
}
embed
.setTitle(match.name+" (" + match.poitype + ")")
.setImage(mapurl)
.setDescription(description);
if(match.image.length > 0) {
// Embed a thumbnail of the POI.
embed.setThumbnail(match.image)
}
clientMessage = {embed};
} else if(matches.length <= maxHits) {
clientMessage = strings[config.language]["selectmap"];
clientMessage += "\n";
clientMessage += poifinder.listResults(matches).join("\n");
// Too long messages will be rejected by server
if(clientMessage.length > MAX_MESSAGE_SIZE ) {
clientMessage = strings[config.language]["toomany"].replace('{num}', matches.length);
clientMessage += strings[config.language]["refinequery"];
}
} else {
clientMessage = strings[config.language]["toomany"].replace('{num}', matches.length);
if (!isDirectMessage && matches.length < MAX_HITS_DM)
clientMessage += strings[config.language]["senddm"];
else
clientMessage += strings[config.language]["refinequery"];
}
message.channel.send(clientMessage)
.then(function(msg) {
writeLog("searches.log", " " + message.channel.name +": ["+ matches.length + "] " + message);
}).catch(function(error) {
writeLog("error.log", "ERROR: " + message.channel.name +":" + message);
});
}
});
client.on("ready", () => {
// Only set activity if config says so (helps if Pidgey is runing together with other bots).
if(config.set_activity) client.user.setActivity(config.prefix + config.command);
console.log("Pidgey is ready and knows about " + poifinder.poiCount() + " interesting places");
});
client.on('error', (error) => {
writeLog("error.log", error);
});
client.login(config.token);