You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

57 lines
1.3 KiB

import requests
import discord
import asyncio
import os
# https://discordapp.com/api/oauth2/authorize?client_id=575315615427067926&scope=bot
TOKEN = os.environ["TOKEN"]
client = discord.Client()
textchannels = []
members = []
servers = []
destination_channel = "xkcd"
def get_channels_by_name(name):
return [i for i in textchannels if name in i.name]
def get_lastest_num():
resp = requests.get("https://xkcd.com/info.0.json")
return resp.json()['num']
async def loop():
# latest = get_lastest_num()
latest = 1
while True:
tmp = get_lastest_num()
if latest != tmp:
for channel in get_channels_by_name(destination_channel):
try:
await channel.send("New XKCD: https://xkcd.com/{}".format(tmp))
except discord.errors.Forbidden as e:
print("An error has occured: {}".format(e))
latest = tmp
await asyncio.sleep(30)
@client.event
async def on_ready():
for server in client.guilds:
servers.append(server)
for channel in server.channels:
textchannels.append(channel)
for member in server.members:
members.append(member)
print("bot ready")
client.loop.create_task(loop())
if __name__ == "__main__":
client.run(TOKEN)