Skip to content

Commit b4961a8

Browse files
committed
xkcd: check output consistency
* replaced `sopel.module` by `sopel.plugin` * used `plugin.output_prefix` with `bot.say` method And in theory, I'm done!
1 parent c34f743 commit b4961a8

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

sopel/modules/xkcd.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
import requests
1717

18-
from sopel.module import commands, url
18+
from sopel import plugin
1919
from sopel.modules.search import bing_search
2020

21+
PLUGIN_OUTPUT_PREFIX = '[xkcd] '
2122

2223
ignored_sites = [
2324
# For searching the web
@@ -53,7 +54,8 @@ def web_search(query):
5354
return match.group(1)
5455

5556

56-
@commands('xkcd')
57+
@plugin.command('xkcd')
58+
@plugin.output_prefix(PLUGIN_OUTPUT_PREFIX)
5759
def xkcd(bot, trigger):
5860
""".xkcd - Finds an xkcd comic strip.
5961
@@ -89,7 +91,7 @@ def xkcd(bot, trigger):
8991
else:
9092
number = web_search(query)
9193
if not number:
92-
bot.say('Could not find any comics for that query.')
94+
bot.reply('Could not find any comics for that query.')
9395
return
9496
requested = get_info(number)
9597

@@ -99,12 +101,12 @@ def xkcd(bot, trigger):
99101
def numbered_result(bot, query, latest, commanded=True):
100102
max_int = latest['num']
101103
if query > max_int:
102-
bot.say(("Sorry, comic #{} hasn't been posted yet. "
103-
"The last comic was #{}").format(query, max_int))
104+
bot.reply(("Sorry, comic #{} hasn't been posted yet. "
105+
"The last comic was #{}").format(query, max_int))
104106
return
105107
elif query <= -max_int:
106-
bot.say(("Sorry, but there were only {} comics "
107-
"released yet so far").format(max_int))
108+
bot.reply(("Sorry, but there were only {} comics "
109+
"released yet so far").format(max_int))
108110
return
109111
elif abs(query) == 0:
110112
requested = latest
@@ -121,20 +123,26 @@ def numbered_result(bot, query, latest, commanded=True):
121123

122124

123125
def say_result(bot, result, commanded=True):
124-
message = '{}{} | Alt-text: {}'.format(
125-
result['url'] + ' | ' if commanded else '',
126-
result['title'], result['alt']
127-
)
128-
bot.say(message)
126+
parts = []
129127

128+
if commanded:
129+
parts.append(result['url'])
130130

131-
@url(r'xkcd.com/(\d+)')
131+
parts.append(result['title'])
132+
parts.append('Alt-text: %s' % result['alt'])
133+
134+
bot.say(' | '.join(parts))
135+
136+
137+
@plugin.url(r'xkcd.com/(\d+)')
138+
@plugin.output_prefix(PLUGIN_OUTPUT_PREFIX)
132139
def get_url(bot, trigger, match):
133140
latest = get_info()
134141
numbered_result(bot, int(match.group(1)), latest, commanded=False)
135142

136143

137-
@url(r'https?://xkcd\.com/?$')
144+
@plugin.url(r'https?://xkcd\.com/?$')
145+
@plugin.output_prefix(PLUGIN_OUTPUT_PREFIX)
138146
def xkcd_main_page(bot, trigger, match):
139147
latest = get_info()
140148
numbered_result(bot, 0, latest, commanded=False)

0 commit comments

Comments
 (0)