user15206020
user15206020

Reputation: 25

How do i link files in python?

So my example is, I'm writing a Discord bot so that when I type a command in chat it will return the corresponding value.

Now I'm starting off with ammo for this game, the problem being there is a fair few different types of ammo in the game. Since I am new to coding and Python I wanted to get into the habit of separating them into different .py files so my codes isn't all messed and i can jump between them easily and keep track of everything down the line.

In my main file there isn't anything but import shotgun_1, shotgun_2. What I noticed, though, is that only shotgun_1 commands will be recognized; shotgun_2 doesn't work.

If I remove shotgun_1 and have only import shotgun_2 then the shotgun_2 code will work and send in the chat "HI!". If i remove shotgun_2 and only use shotgun_1 then the commands of shotgun_1 work.

Is there a way to somehow link these two files so they execute the same time?

shotgun_1.py

import discord
import os
import shotgun_2

client = discord.Client()

@client.event
async def on_ready():
   print('We have logged in as {0.user}'.format(client))
       
@client.event
async def on_message(message):
   if message.author == client.user:
       return

   if message.content.startswith('1270 all'):
    await message.channel.send('12/70 **5.25mm Buckshot** ``` DAMAGE: 8x37  PenPower: 1  Armor Damage: 15%```')
    await message.channel.send('12/70 **8.5 mm "Magnum" Buckshot** ```DAMAGE: 8x50  PenPower: 2 Armor Damage: 26%```')
    await message.channel.send('12/70 **6.5 mm "Express" Buckshot** ```DAMAGE: 9x35  PenPower: 3 Armor Damage: 26%```')
    await message.channel.send('12x70 **7mm Buckshot** ```DAMAGE: 8x39  PenPower: 3 Armor Damage:26%```')
    await message.channel.send('12/70 **Flechette** ```DAMAGE: 8x25  PenPower: 31 Armor Damage: 26%```')
    await message.channel.send('12x70 **RIP** ```DAMAGE: 265  PenPower: 2 Armor Damage: 11%```')
    await message.channel.send('12/70 **HP Slug "SuperFormance"** ```DAMAGE: 220  PenPower: 5 Armor Damage: 12%```')
    await message.channel.send('12/70 **Grizzly 40 Slug** ```DAMAGE: 190  PenPower: 12 Armor Damage: 48%```')
    await message.channel.send('12/70 **HP Slug Copper Sabot Premier** ```DAMAGE: 206  PenPower: 14 Armor Damage: 46%```')
    await message.channel.send('12x70 **Led slug** ```DAMAGE: 167  PenPower: 15 Armor Damage: 55%```')
    await message.channel.send('12x70 **"Poleva-3" Slug** ```DAMAGE: 140  PenPower: 17 Armor Damage: 40%```')
    await message.channel.send('12x70 **Dual Sabot Slug** ```DAMAGE: 2x85 PenPower: 17 Armor Damage: 65%```')
    await message.channel.send('12x70 **FTX Custom LIte Slug** ```DAMAGE: 183  PenPower: 20 Armor Damage: 50%```')
    await message.channel.send('12x70 **"Poleva-6u" Slug** ```DAMAGE: 150  PenPower: 20 Armor Damage: 50%```')
    await message.channel.send('12x70 **shell with .50 BMG bullet** ```DAMAGE: 197  PenPower: 26 Armor Damage: 57%```')
    await message.channel.send('12x70 **AP-20 Slug** ```DAMAGE: 164  PenPower: 37 Armor Damage: 65%```')
   if message.content.startswith('5m buck'):
    await message.channel.send('12/70 **5.25mm Buckshot** ``` DAMAGE: 8x37  PenPower: 1  Armor Damage: 15%```')
   if message.content.startswith('1270 magnum'):
    await message.channel.send('12/70 **8.5 mm "Magnum" Buckshot** ```DAMAGE: 8x50  PenPower: 2 Armor Damage: 26%```')     
   if message.content.startswith('1270 express'):
    await message.channel.send('12/70 **6.5 mm "Express" Buckshot** ```DAMAGE: 9x35  PenPower: 3 Armor Damage: 26%```')
   if message.content.startswith(' 7m buck'):
    await message.channel.send('12x70 **7mm Buckshot** ```DAMAGE: 8x39  PenPower: 3 Armor Damage:26%```')
   if message.content.startswith('1270 Flechette'):
    await message.channel.send('12/70 **Flechette** ```DAMAGE: 8x25  PenPower: 31 Armor Damage: 26%```')
   if message.content.startswith('1270 rip'):
    await message.channel.send('12x70 **RIP** ```DAMAGE: 265  PenPower: 2 Armor Damage: 11%```')     
   if message.content.startswith('1270 hp Super'):
    await message.channel.send('12/70 **HP Slug "SuperFormance"** ```DAMAGE: 220  PenPower: 5 Armor Damage: 12%```')
   if message.content.startswith('1270 led'):
    await message.channel.send('12x70 **Led slug** ```DAMAGE: 167  PenPower: 15 Armor Damage: 55%```') 
   if message.content.startswith('1270 poleva 3'):
    await message.channel.send('12x70 **"Poleva-3" Slug** ```DAMAGE: 140  PenPower: 17 Armor Damage: 40%```')
   if message.content.startswith('1270 sabot'):
    await message.channel.send('12x70 **Dual Sabot Slug** ```DAMAGE: 2x85 PenPower: 17 Armor Damage: 65%```')     
   if message.content.startswith('1270 lite'):
    await message.channel.send('12x70 **FTX Custom LIte Slug** ```DAMAGE: 183  PenPower: 20 Armor Damage: 50%```')
   if message.content.startswith('1270 poleva 6'):
    await message.channel.send('12x70 **"Poleva-6u" Slug** ```DAMAGE: 150  PenPower: 20 Armor Damage: 50%```')
   if message.content.startswith('1270 50 bmg'):
    await message.channel.send('12x70 **shell with .50 BMG bullet** ```DAMAGE: 197  PenPower: 26 Armor Damage: 57%```')
   if message.content.startswith('1270 ap20'):
    await message.channel.send('12x70 **AP-20 Slug** ```DAMAGE: 164  PenPower: 37 Armor Damage: 65%```')
   if message.content.startswith('1270 Grizzly'): 
    await message.channel.send('12/70 **Grizzly 40 Slug** ```DAMAGE: 190  PenPower: 12 Armor Damage: 48%```')
   if message.content.startswith('1270 HP copper'): 
    await message.channel.send('12/70 **HP Slug Copper Sabot Premier** ```DAMAGE: 206  PenPower: 14 Armor Damage: 46%```')

client.run(os.getenv('TOKEN'))

shotgun_2.py

import discord

client = discord.Client()
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('hello'):
        await message.channel.send('Hi!')

If anyone can help i would really appreciate it!

Upvotes: 1

Views: 126

Answers (2)

Ali Samji
Ali Samji

Reputation: 479

The client.run that is necessary at the end of your file is a synchronous operation (i.e. it blocks the process). When you do import shotgun_1, shotgun_2, Python never finishes loading shotgun_1 since it is still running, so it never gets to load shotgun_2.

In order for both of these commands to run, you would have to put them in the same on_message function.

Upvotes: 1

Jacob Lee
Jacob Lee

Reputation: 4700

I would suggest not separating these two aspects into different files. shotgun_2.py does not contain any code that could not be put into shotgun_1.py. This is also why shotgun_2.py doesn't do anything when shotgun_1.py runs. Your current method imports shotgun_2.py, but it doesn't do anything after it imports. I cut out large chunks of code, but here are my edits:

import discord
import os

@client.event
async def on_ready():
   print('We have logged in as {0.user}'.format(client))
       
@client.event
async def on_message(message):
   if message.author == client.user:
       return

    if message.content.startswith("hello"):
        await message.channel.send("Hi!")

   --snip--

client.run(os.getenv('TOKEN'))

This should allow the bot to run properly, as you intended it to.

While it is outside the scope of this question, I would recommend some re-formatting of all your if statements. I would recommend using a dictionary to store all the possible responses and the corresponding messages to send.

For example:

ammo_types = {
    "5m buck": "12/70 **5.25mm Buckshot** ``` DAMAGE: 8x37  PenPower: 1  Armor Damage: 15%```",
    "1270 magnum": '12/70 **8.5 mm "Magnum" Buckshot** ```DAMAGE: 8x50  PenPower: 2 Armor Damage: 26%```",
    ...
}

Then, inside on_message, you could simplify all the if statements.

if message.content.startswith("1270 all"):
    for opt in ammo_types.values():
        await message.channel.send(opt)
else:
    for ammo in ammo_types:
        if message.content.startswith(ammo):
            await message.channel.send(ammo_types[ammo])

Upvotes: 1

Related Questions