Last active
August 10, 2021 07:42
-
-
Save Mesabloo/10850fe172001f4900594962fa9e1010 to your computer and use it in GitHub Desktop.
A messaging app-like bot, reordering the channels based on activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from discord.ext import commands | |
import discord | |
import sqlite3 | |
prefix = '#!' | |
client = commands.Bot(command_prefix=prefix) | |
print('Connecting to database...') | |
conn = sqlite3.connect('categories.db') | |
c = conn.cursor() | |
c.execute('CREATE TABLE IF NOT EXISTS categories(id INTEGER UNIQUE);') | |
conn.commit() | |
@client.event | |
async def on_ready(): | |
print(f'Logged in as {client.user}!') | |
@client.event | |
async def on_message(m): | |
if m.channel.type == discord.ChannelType.private or m.channel.type == discord.ChannelType.group: | |
return | |
await client.process_commands(m) | |
category_id = m.channel.category_id | |
c = conn.cursor() | |
rows = c.execute('SELECT * FROM categories WHERE id = ?', (category_id,)).fetchall() | |
if not rows: | |
return | |
channel_category = m.guild.get_channel(category_id) | |
actual_channel = m.channel | |
channels = channel_category.text_channels | |
actual_index = channels.index(actual_channel) | |
to_sort = [c for i, c in enumerate(channels) if i < actual_index] | |
to_sort.reverse() | |
for c in to_sort: | |
await c.edit(position=c.position + 1) | |
await actual_channel.edit(position=1) | |
@client.event | |
async def on_command_error(context, ex): | |
if context.command: | |
await context.channel.send(f'Error in command `{prefix}{context.command}`: ```py\n{str(ex)}```') | |
@client.command(description='Registers a channel category to sort the channels in') | |
async def register(ctx, category_id: int): | |
perms = ctx.author.guild_permissions | |
if not perms.manage_guild and not perms.manage_channels: | |
await ctx.channel.send('You don\'t have the required permissions to perform this command!') | |
return | |
chan = ctx.guild.get_channel(category_id) | |
if not chan: | |
await ctx.channel.send(f'Category <#{category_id}> not found in current server.') | |
return | |
if chan.type != discord.ChannelType.category: | |
await ctx.channel.send(f'Channel <#{category_id}> is not a category.') | |
return | |
c = conn.cursor() | |
try: | |
c.execute('INSERT OR ABORT INTO categories (id) VALUES (?)', (category_id,)) | |
conn.commit() | |
await ctx.channel.send(f'Successfully registered category **<#{category_id}>**!') | |
except: | |
await ctx.channel.send(f'Already registered category **<#{category_id}>**.') | |
@client.command(description='Unregisters a channel category') | |
async def unregister(ctx, category_id: int): | |
perms = ctx.author.guild_permissions | |
if not perms.manage_guild and not perms.manage_channels: | |
await ctx.channel.send('You don\'t have the required permissions to perform this command!') | |
return | |
chan = ctx.guild.get_channel(category_id) | |
if not chan: | |
await ctx.channel.send(f'Category <#{category_id}> not found in current server.') | |
return | |
if chan.type != discord.ChannelType.category: | |
await ctx.channel.send(f'Channel <#{category_id}> is not a category.') | |
c = conn.cursor() | |
try: | |
c.execute('DELETE FROM categories WHERE id = ?', (category_id,)) | |
if c.rowcount == 0: | |
raise Exception() | |
conn.commit() | |
await ctx.channel.send(f'Successfully unregistered category **<#{category_id}>**!') | |
except: | |
await ctx.channel.send(f'Category **<#{category_id}>** not registered.') | |
@client.command(description='Shows registered categories on the current server') | |
async def registered(ctx): | |
c = conn.cursor() | |
chans = c.execute('SELECT * FROM categories').fetchall() | |
chans = [c_ for c_, in chans if ctx.guild.get_channel(c_)] | |
if not chans: | |
await ctx.channel.send('There are no categories registered...') | |
return | |
for c in chans: | |
await ctx.channel.send(f'Category **<#{c}>** is registered and handled.') | |
TOKEN = ''' Put your bot token here ''' | |
client.run(TOKEN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment