Conditions | 22 |
Total Lines | 91 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like main.on_message() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # Copyright 2017 Starbot Discord Project |
||
114 | @client.event |
||
115 | async def on_message(message_in): |
||
116 | # Ignore messages that aren't from a server and from ourself. |
||
117 | #if not message_in.guild: |
||
118 | # return |
||
119 | if message_in.author.id == client.user.id: |
||
120 | return |
||
121 | if message_in.author.bot: |
||
122 | return |
||
123 | |||
124 | is_command = False |
||
125 | |||
126 | # Get prefix. If not on a server, no prefix is needed. |
||
127 | #logging.message_log(message_in, message_in.guild.id) |
||
128 | if message_in.guild: |
||
129 | prefix = settings.prefix_get(message_in.guild.id) |
||
130 | me = message_in.guild.me |
||
131 | else: |
||
132 | prefix = "" |
||
133 | me = message_in.channel.me |
||
134 | |||
135 | |||
136 | # Should we die? Check for exit command. |
||
137 | if message_in.content == prefix + "exit" or message_in.content == "{} exit".format(me.mention): |
||
138 | if settings.owners_check(message_in.author.id): |
||
139 | sys.exit(0) |
||
140 | |||
141 | # Check for cache contents command. |
||
142 | if message_in.content.startswith(prefix + "cachecontents") or message_in.content.startswith("{} cachecontents".format(me.mention)): |
||
143 | cacheCount = glob.glob("cache/{}_*".format(message_in.content.split(' ')[-1])) |
||
144 | cacheString = '\n'.join(cacheCount) |
||
145 | await message_in.channel.send(message_in.channel, "```{}```".format(cacheString)) |
||
146 | |||
147 | # Check each command loaded. |
||
148 | for command in Bot.commands: |
||
149 | # Do we have a command? |
||
150 | if command_api.is_command(message_in, prefix, command): |
||
151 | # Prevent message count increment. |
||
152 | is_command = True |
||
153 | |||
154 | # Send typing message. |
||
155 | async with message_in.channel.typing(): |
||
156 | # Build message object. |
||
157 | message_recv = message.Message |
||
158 | message_recv.command = command.name |
||
159 | if message_in.content.startswith("{} ".format(me.mention)): |
||
160 | message_recv.body = message_in.content.split("{} ".format(me.mention) + |
||
161 | command.name, 1)[1] |
||
162 | else: |
||
163 | message_recv.body = message_in.content.split(prefix + command.name, 1)[1] |
||
164 | message_recv.author = message_in.author |
||
165 | message_recv.guild = message_in.guild |
||
166 | message_recv.mentions = message_in.mentions |
||
167 | message_recv.channel = message_in.channel |
||
168 | |||
169 | command_result = await command.func(message_recv) |
||
170 | |||
171 | # No message, error. |
||
172 | if not command_result: |
||
173 | await message_in.channel.send( |
||
174 | "**Beep boop - Something went wrong!**\n_Command did not return a result._") |
||
175 | |||
176 | # Do list of messages, one after the other. If the message is more than 5 chunks long, PM it. |
||
177 | elif type(command_result) is list: |
||
178 | if len(command_result) > 5: # PM messages. |
||
179 | # Send message saying that we are PMing the messages. |
||
180 | await message_in.channel.send( |
||
181 | "Because the output of that command is **{} pages** long, I'm just going to PM the result to you.".format(len(command_result))) |
||
182 | |||
183 | # PM it. |
||
184 | for item in command_result: |
||
185 | await process_message(message_in.author, message_in, item) |
||
186 | |||
187 | else: # Send to channel. |
||
188 | for item in command_result: |
||
189 | await process_message(message_in.channel, message_in, item) |
||
190 | |||
191 | # Do regular message. |
||
192 | else: |
||
193 | await process_message(message_in.channel, message_in, command_result) |
||
194 | |||
195 | # Do we delete the message afterwards? |
||
196 | if message_in.guild and command_result.delete: |
||
197 | await client.delete_message(message_in) |
||
198 | |||
199 | # Increment message counters if not command. |
||
200 | if message_in.guild and not is_command: |
||
201 | logging.message_log(message_in, message_in.guild.id) |
||
202 | count = logging.message_count_get(message_in.guild.id) |
||
203 | Bot.messagesSinceStart += 1 |
||
204 | count += 1 |
||
205 | |||
224 |