This repository was archived by the owner on Oct 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpyGunsmith.py
More file actions
183 lines (142 loc) · 7.77 KB
/
Copy pathpyGunsmith.py
File metadata and controls
183 lines (142 loc) · 7.77 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import discord, gunsmith, json, re
print('Bot starting...')
print('Loading configuration file...')
with open('gunsmith-config.json', 'r') as config_file:
config = json.load(config_file)
discord_config = config['discord']
print('Initializing bot...')
gunsmith_definitions = gunsmith.initialize()
class PyGunsmith (discord.Client):
def __init__(self):
super().__init__()
# Events to be trapped by the bot
async def on_ready(self):
print('Bot is ready...')
async def on_message(self, message):
global gunsmith_definitions
if message.content.startswith('!gunsmith'):
input_string = message.content
input_string = input_string.replace('!gunsmith ', '').strip()
if input_string == 'reload':
print('Gunsmith is reloading...')
author_name = str(message.author)
print(author_name)
# ignore requests not delivered from the webhook
# following criteria needs to be moved to config file
if 'gunsmith' in author_name or 'dad2cl3' in author_name:
gunsmith_definitions = gunsmith.initialize()
# following message body needs to be moved to config file
await message.channel.send('Careful where you point that thing!')
else:
# following message body needs to be moved to config file
await message.channel.send('First time was incompetence, this time it\'s sabotage. <@297694001874731008> get to the bottom of this.')
else:
discord_id = message.author.id
guild_id = message.guild.id
# testing override
# should prolly move these overrides to config and enable a test mode for the bot.
#guild_id = 141176119813603328
#discord_id = 312310965033238528
#discord_id = 141625016193253377 # GreatDaemon
#discord_id = 102806583133605888
#discord_id = 347395856393043990 # mr_rots
print('Guild ID: {0} - Discord ID: {1}'.format(guild_id, discord_id))
input_string = message.content
input_string = input_string.replace('!gunsmith', '').strip()
print('Input string: {0}'.format(input_string))
if input_string == 'help':
sender_id = message.author.id
user = await client.get_user_info(sender_id)
help_details = config['help']
help_embed = discord.Embed(
title=help_details['title'],
description=help_details['description']
)
help_embed.set_footer(
icon_url=help_details['footer']['icon_url'],
text=help_details['footer']['text']
)
for field in help_details['fields']:
help_embed.add_field(
name=field['name'],
value=field['value'],
inline=False
)
await client.send_message(user, embed=help_embed)
else:
# add ability to override server and discord
# search for server_id: and override local variable
if 'guild_id' in input_string:
guild_id_pattern = re.compile('.*guild_id:([0-9]+)\s?')
matches = re.match(guild_id_pattern, input_string)
if len(matches.regs) == 2:
guild_id = str(matches.group(1))
print('Override guild_id: {0}'.format(guild_id))
# search for discord_id: and override local variable
if 'discord_id' in input_string:
discord_id_pattern = re.compile('.*discord_id:([0-9]+)\s?')
matches = re.match(discord_id_pattern, input_string)
if len(matches.regs) == 2:
discord_id = str(matches.group(1))
print('Override discord_id: {0}'.format(discord_id))
# strip out the overrides from the input string
guild_id_pattern = re.compile('(.*)(guild_id:[0-9]+)(\s?)')
input_string = guild_id_pattern.sub('\\1\\3', input_string)
discord_id_pattern = re.compile('(.*)(discord_id:[0-9]+)(\s?)')
input_string = discord_id_pattern.sub('\\1\\3', input_string)
# clean up any extra spaces
input_string = input_string.strip()
weapon_details = gunsmith.main(guild_id, discord_id, input_string, gunsmith_definitions)
#print(json.dumps(weapon_details)) # debugging
if 'error' in weapon_details:
await message.channel.send(weapon_details['error'])
else:
for weapon_detail in weapon_details['weapon_details']:
if len(input_string) > 0:
content = '<@{0}>: `{1}`'.format(discord_id, input_string)
else:
content = '<@{0}>'.format(discord_id)
#print(json.dumps(weapon_detail)) # debugging
weapon_embed = discord.Embed(
title=weapon_detail['details']['name'],
description=weapon_detail['details']['description']
)
if len(weapon_detail['details']['screenshot']) > 0:
weapon_embed.set_image(
url=weapon_detail['details']['screenshot']
)
weapon_embed.set_thumbnail(
url=weapon_detail['details']['icon']
)
if len(weapon_detail['source']) > 0:
weapon_embed.add_field(
name='Source(s)',
value=weapon_detail['source'],
inline=False
)
'''weapon_embed.add_field(
name='Stats',
value=weapon_detail['stat_names'],
inline=True
)
weapon_embed.add_field(
name='Values',
value=weapon_detail['stat_values'],
inline=True
)'''
weapon_embed.add_field(
name='Stats',
value=weapon_detail['stats']
)
weapon_embed.add_field(
name='Perks',
value=weapon_detail['perks'],
inline=False
)
weapon_embed.set_footer(
text='API Time: {0} - Elapsed Time: {1}'.format(weapon_details['api_time'], weapon_details['elapsed_time'])
)
print(json.dumps(weapon_embed.to_dict())) # debugging
await message.channel.send(content=content, embed=weapon_embed)
client = PyGunsmith()
client.run(discord_config['token'])