here.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import discord.ext.commands as discord_cmds
  2. import myanimebot.utils as utils
  3. import myanimebot.globals as globals
  4. import myanimebot.commands.permissions as permissions
  5. @discord_cmds.command(name="here")
  6. @discord_cmds.check(permissions.in_allowed_role)
  7. async def here_cmd(ctx):
  8. ''' Processes the command "here" and registers a channel to send new found feeds '''
  9. server = ctx.guild
  10. channel = ctx.channel
  11. if utils.is_server_in_db(server.id):
  12. # Server in DB, so we need to update the channel
  13. # Check if channel already registered
  14. channels = utils.get_channels(server.id)
  15. channels_id = [channel["channel"] for channel in channels]
  16. globals.logger.debug("Channels {} and channel id {}".format(channels_id, channel.id))
  17. if (str(channel.id) in channels_id):
  18. await ctx.send("Channel **{}** already in use for this server.".format(channel))
  19. else:
  20. cursor = globals.conn.cursor(buffered=True)
  21. cursor.execute("UPDATE t_servers SET channel = {} WHERE server = {}".format(channel.id, server.id))
  22. globals.conn.commit()
  23. cursor.close()
  24. await ctx.send("Channel updated to: **{}**.".format(channel))
  25. else:
  26. # No server found in DB, so register it
  27. cursor = globals.conn.cursor(buffered=True)
  28. cursor.execute("INSERT INTO t_servers (server, channel) VALUES ({},{})".format(server.id, channel.id))
  29. globals.conn.commit() # TODO Move to corresponding file
  30. await ctx.send("Channel **{}** configured for **{}**.".format(channel, server))