-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot-main.py
95 lines (87 loc) · 3.27 KB
/
bot-main.py
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
# Imports the modules for operation and gets the key from the environment
import discord
from discord.ext import commands
import qwant
from time import sleep
from os import environ
key = environ.get('discordKey')
bot = commands.Bot(command_prefix='xkcd ')
bot.remove_command('help')
searchList = []
searchListIndice = 0
search = ''
searchResult = ''
postMessage = ''
# Tells me when the bot logs in.
@bot.event
async def on_ready():
print('Bot logged in.')
# Controls the bot's fetch command
@bot.command()
async def fetch(ctx, arg):
if arg == 'latest':
await ctx.send('https://xkcd.com')
else:
await ctx.send('https://xkcd.com/%s' % str(arg))
#Controls the bot's find command
@bot.command()
async def find(ctx, *, arg):
global search
global searchList
global searchListIndice
global searchResult
global postMessage
global preMessage
preMessage = await ctx.send('`Searching for xkcd comic...`')
query = f'site:xkcd.com {arg}'
searchList = qwant.items(query, count=10)
searchListIndice = 0
search = searchList[0]
search = str(search['url'])
search = search.replace('www.', '')
print(search)
searchResult = await ctx.send(search)
postMessage = await ctx.send('`Wrong comic? React with ❎ to tell me!`')
await postMessage.add_reaction('❎')
@bot.event
async def on_reaction_add(reaction, user):
global searchList
global searchListIndice
global search
global searchResult
global postMessage
global preMessage
channel = reaction.message.channel
if reaction.emoji == '❎' and user.name != 'XKCDiscord':
searchListIndice = searchListIndice + 1
if searchListIndice > len(searchList):
await channel.send('Sorry, that\'s all the comics I have! Try a different search term')
else:
await searchResult.delete()
await postMessage.delete()
search = searchList[searchListIndice]
search = str(search['url'])
search = search.replace('www.', '')
searchResult = await channel.send(search)
postMessage = await channel.send('`Wrong comic? React with ❎ to tell me`')
await postMessage.add_reaction('❎')
@bot.command()
async def help(ctx):
embed = discord.Embed(
color=discord.Colour.purple(),
title='XKCDiscord Help',
description='Thank you for using XKCDiscord! Here are some commands to help you along the way',
)
embed.set_thumbnail(url='XKCDiscord Help.png')
embed.set_author(name='XKCDiscord, by CmdrNorthpaw', icon_url='https://cdn.discordapp.com/avatars/662979775060639751/7a44d391e0bd8c02ce9a00b7e7b53b3e.png')
embed.add_field(
name='xkcd fetch',
value='This command takes the cartoon number as an argument (say 1646) and posts that comic to chat. You can also fetch the latest comic with `xkcd fetch latest`'
)
embed.add_field(
name='xkcd find',
value='This command is used to search for a cartoon (say twitter bot). Perfect for when you know what cartoon you want but you can\'t remember the exact number. Don\'t worry if it doesn\'t get the right cartoon first time, just click the ❎ to see the next result'
)
await ctx.send(embed=embed)
# Actually runs the bot, using the key from line 6 as an argument
bot.run(key)