├── guild-album-mapping.csv ├── .gitignore ├── README.md ├── LICENSE └── banner-changer.py /guild-album-mapping.csv: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | images/ 2 | guild-album-mapping.csv -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Discontinued 2 | 3 | *** 4 | 5 | This project is discontinued. I have developed a new bot which can found here: 6 | 7 | :bangbang: | Please use https://github.com/NotNorom/discord-banner-bot 8 | :---: | :--- 9 | 10 | 11 | *** 12 | 13 | Old docs: 14 | 15 | ## setalbum 16 | ?setalbum IMGUR_ALBUM_LINK 17 | 18 | This will set the album to choose the random banner images from. 19 | Images will be choosen randomly every 24h. 20 | 21 | 22 | ## seticon 23 | ?seticon IMAGE_LINK 24 | 25 | This will set the server icon. 26 | Image link needs to be .png or .jpg! 27 | 28 | 29 | ## setbanner 30 | ?setbanner IMAGE_LINK 31 | 32 | This will set the server banner. 33 | Image link needs to be .png or .jpg! 34 | 35 | 36 | ## geticon 37 | ?geticon 38 | 39 | This will return a link to the current server icon. 40 | 41 | 42 | ## getbanner 43 | ?getbanner 44 | 45 | This will return a link to the current server banner. 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /banner-changer.py: -------------------------------------------------------------------------------- 1 | import os 2 | import io 3 | import csv 4 | import random 5 | import asyncio 6 | import aiohttp 7 | import logging 8 | import discord 9 | from discord.ext import tasks, commands 10 | from imgur_downloader import ImgurDownloader 11 | 12 | CSV_NAME = "guild-album-mapping.csv" 13 | BOT_TOKEN = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" 14 | IMGUR_DOWNLOAD_PATH = "images" 15 | 16 | logging.basicConfig(level=logging.INFO) 17 | 18 | description = '''An banner changer bot''' 19 | bot = commands.Bot(command_prefix='?', description=description) 20 | 21 | 22 | @bot.event 23 | async def on_ready(): 24 | await bot.change_presence(activity=discord.Game(name='aaaaaaaaaaa')) 25 | print("logged in: ", bot.user) 26 | 27 | 28 | @bot.command(description="Set the guild banner image") 29 | async def setbanner(ctx, url: str): 30 | """Set the guild banner image.""" 31 | if ctx.message.guild is None: 32 | return 33 | 34 | permissions = ctx.message.author.permissions_in(ctx.channel) 35 | if not permissions.administrator: 36 | print("user is not admin") 37 | return 38 | 39 | async with aiohttp.ClientSession() as session: 40 | async with session.get(url) as resp: 41 | if resp.status != 200: 42 | return await ctx.send('Could not download file...') 43 | data = io.BytesIO(await resp.read()) 44 | await ctx.message.guild.edit(banner=data.read()) 45 | ctx.send("Banner set!") 46 | 47 | 48 | @bot.command(description="Get the guild banner image") 49 | async def getbanner(ctx): 50 | """Get the guild banner image.""" 51 | if ctx.message.guild is None: 52 | return 53 | await ctx.send(ctx.message.guild.banner_url) 54 | 55 | 56 | @bot.command(description="Set the guild icon") 57 | async def seticon(ctx, url: str): 58 | """Set the guild icon.""" 59 | if ctx.message.guild is None: 60 | return 61 | 62 | permissions = ctx.message.author.permissions_in(ctx.channel) 63 | if not permissions.administrator: 64 | print("user is not admin") 65 | return 66 | 67 | async with aiohttp.ClientSession() as session: 68 | async with session.get(url) as resp: 69 | if resp.status != 200: 70 | return await ctx.send('Could not download file...') 71 | data = io.BytesIO(await resp.read()) 72 | await ctx.message.guild.edit(icon=data.read()) 73 | await ctx.send("Icon set!") 74 | 75 | 76 | @bot.command(description="Get the guild icon") 77 | async def geticon(ctx): 78 | """Get the guild icon.""" 79 | if ctx.message.guild is None: 80 | return 81 | await ctx.send(ctx.message.guild.icon_url) 82 | 83 | 84 | @bot.command(description="Set imgur album/gallery to use images from") 85 | async def setalbum(ctx, url): 86 | """Set imgur album/gallery and download them""" 87 | if ctx.message.guild is None: 88 | return 89 | 90 | permissions = ctx.message.author.permissions_in(ctx.channel) 91 | if not permissions.administrator: 92 | print("user is not admin") 93 | return 94 | 95 | mapping = {} 96 | # load all mappings into our program 97 | with open(CSV_NAME, newline='') as f: 98 | reader = csv.reader(f) 99 | for row in reader: 100 | # guild.id = imgur link 101 | if len(row) == 2: 102 | mapping[int(row[0])] = row[1] 103 | 104 | # replace our current one 105 | mapping[ctx.message.guild.id] = url 106 | print("downloaded images:", await download_album_for_guild(ctx.message.guild.id, url)) 107 | 108 | # write the new values back to the file 109 | with open(CSV_NAME, "w", newline='') as f: 110 | writer = csv.writer(f) 111 | for key, value in mapping.items(): 112 | writer.writerow([key, value]) 113 | print(mapping) 114 | 115 | 116 | async def download_album_for_guild(guild_id, url): 117 | for file in os.listdir(os.path.join(os.path.curdir, IMGUR_DOWNLOAD_PATH, str(guild_id))): 118 | file_path = os.path.join(os.path.curdir, IMGUR_DOWNLOAD_PATH, str(guild_id), file) 119 | try: 120 | if os.path.isfile(file_path): 121 | os.unlink(file_path) 122 | except Exception as e: 123 | print(e) 124 | 125 | return ImgurDownloader(url, os.path.join(os.path.curdir, IMGUR_DOWNLOAD_PATH), file_name=str(guild_id), delete_dne=True).save_images() 126 | 127 | 128 | async def set_random_icon_for_guild(guild_id): 129 | # https://stackoverflow.com/questions/701402/best-way-to-choose-a-random-file-from-a-directory 130 | image_path = random.choice([f for f in os.listdir(os.path.join(os.path.curdir, IMGUR_DOWNLOAD_PATH, str(guild_id))) if os.path.isfile(os.path.join(os.path.curdir, IMGUR_DOWNLOAD_PATH, str(guild_id), f))]) 131 | image_path = os.path.join(os.path.curdir, IMGUR_DOWNLOAD_PATH, str(guild_id), image_path) 132 | guild = bot.get_guild(guild_id) 133 | print("setting image:", image_path, guild_id) 134 | with open(image_path, 'rb') as data: 135 | try: 136 | await guild.edit(icon=data.read()) 137 | except Exception as e: 138 | print(e) 139 | 140 | 141 | async def set_random_banner_for_guild(guild_id): 142 | # https://stackoverflow.com/questions/701402/best-way-to-choose-a-random-file-from-a-directory 143 | image_path = random.choice([f for f in os.listdir(os.path.join(os.path.curdir, IMGUR_DOWNLOAD_PATH, str(guild_id))) if os.path.isfile(os.path.join(os.path.curdir, IMGUR_DOWNLOAD_PATH, str(guild_id), f))]) 144 | image_path = os.path.join(os.path.curdir, IMGUR_DOWNLOAD_PATH, str(guild_id), image_path) 145 | guild = bot.get_guild(guild_id) 146 | print("setting image:", image_path, guild_id) 147 | with open(image_path, 'rb') as data: 148 | try: 149 | await guild.edit(banner=data.read()) 150 | except Exception as e: 151 | print(e) 152 | 153 | 154 | @tasks.loop(hours=12.0) 155 | async def main(): 156 | with open(CSV_NAME, newline='') as f: 157 | reader = csv.reader(f) 158 | for row in reader: 159 | await asyncio.sleep(random.randint(0, 10)) 160 | try: 161 | await set_random_banner_for_guild(int(row[0])) 162 | except IOError as e: 163 | guild = bot.get_guild(row[0]) 164 | print(e, guild) 165 | 166 | 167 | @main.before_loop 168 | async def before_main(): 169 | print("waiting for bot to become ready") 170 | await bot.wait_until_ready() 171 | 172 | 173 | main.start() 174 | bot.run(BOT_TOKEN) 175 | --------------------------------------------------------------------------------