0

I have a script for a discord bot that I need to use in a Django App so I can reference the DB and Models from it. The goal is to dynamically output messages to discord based on records.

In Rails with the discord gem: https://github.com/discordrb/discordrb

I am able to add my script to the initializers folder. This way it starts up and it's always listening. When something like a message or command comes through, I can dynamically update the output message based on the information from the command or message from Discord.

Here is my app\config\initializers\discord_bot.rb file:

require 'discordrb'

bot = Discordrb::Bot.new token: '<>'

bot.message(with_text: 'Ping!') do |event|
  task = Task.find(1)
  event.respond "Pong! #{task.title}"
end

bot.run

I can access the models from here. This is a quick example but if I wanted, I can check against the discord information I receive to dynamically respond with the bot information from the database records.

How am I able to do something similar to this in Django?

uno
  • 1,421
  • 12
  • 38
  • For reference for anyone who sees this and doesn't really know much Rails. The initializer folder is just a folder containing any code that will be loaded _first_ in the application. By the way doing a search for "Django initialization" yields some results such as this: https://stackoverflow.com/questions/1116948/django-initialization does that help solve your problem? – max pleaner Apr 07 '20 at 21:37
  • Thanks Max. Unfortunately my attempts at putting `import discord` into settings.py or __init__.py aren't working. Keep receiving `ModuleNotFoundError: No module named 'discord'` even though this works as a standalone `.py` file – uno Apr 07 '20 at 22:07
  • I have tried adding the code in many areas -- although, I'm not as versed in Django. But others who I have asked are saying things like to concurrently start django and the script together in one and then use an API to send over the database information --- but this seems like overkill. there has to be an easier way – uno Apr 07 '20 at 22:08

0 Answers0