Discord.py: Splitting Async Functionality Between Different Files

Created on 5 Jan 2017  路  2Comments  路  Source: Rapptz/discord.py

Hello this is probably a basic question. What I am trying to do is organise my small bot project. Right now I have 3 files. One bot "settings" script and 2 "game" scripts. The bot "settings" script is the one I am using to initialize my bot and the other 2 scripts are just 2 simple games which are waiting for some user to type $game or $game1. The thing is I don't know where to put client.run() inside this structure. If I put it on the bot then only the message of the bot will work and the games won't run if the user presses game or game1. If I put it in either game then only those games will work and my bot hello message won't work.

I read in the documentation that you must put the client.run() on the end of the script but what happens if you split your program into seperate files?
Here is my current structure:
bot.py:

import discord
import asyncio
from casinogames import on_message
from casinotestgames import on_message

"""
DISCORD BOT SETTINGS
"""
client = discord.Client()

Startup Message

@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print ('Bot working properly')
print('------')

casinogames.py:

import discord
import random
import asyncio
import collections
from random import randint

client = discord.Client()

game here

casinotestgames.py:
Same as above with the client.run() in the end.

With this configuration when I run bot the prints won't show up but the game on casinotestgames will run. If I put the client.run() on casinogames then that game will run. What I want is when I run the bot is to be able to "listen" to all 3 scripts. How would I go about doing that?

Most helpful comment

See the cogs example.

All 2 comments

See the cogs example.

Just came to answer that I found a solution by doing
from casinogames import *
and it works. Your solution seems a lot better though will look into it thanks for the input!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adhoc92 picture adhoc92  路  3Comments

AraHaan picture AraHaan  路  3Comments

j0hnmeow picture j0hnmeow  路  3Comments

tairabiteru picture tairabiteru  路  3Comments

jzburda picture jzburda  路  3Comments