Discord.py Import commands from another py - python

im trying to Import commands from another PY file. But it always Says command not exists...
This is my code in the file named test.py
from discord import Embed
from random import choice
from colorama import *
from os import path
from plyer import notification
intents = discord.Intents().all()
client = commands.Bot(command_prefix = ">", intents=intents)
#commands.command()
async def haha(ctx):
await ctx.send("hi")
import customcmd
client.run("censored")
The code in customcmd.py is
import discord
from discord.ext import commands, tasks
#commands.command()
async def hi(ctx):
await ctx.send(hi)#

Try this
from .customcmd import *

Related

on_message not being triggered when using interactions.Client

I'm using Interactions.py (client = interactions.Client) so that I can use its sophisticated slash commands system, but as a result the on_message event method is no longer triggered. When I use Discord.py (client = discord.Client) the on_message method works successfully.
How do I get on_message to work while using the slash command system of Interactions.py?
import os
import os.path
import interactions
import mysql.connector
import ast
from asyncio.windows_events import NULL
import operator as op
import discord
import inspect
from math import sqrt
from dotenv import load_dotenv
intents = discord.Intents
intents.messages = True
client = interactions.Client(token=TOKEN)
#client.command(
#command details here
)
async def count(ctx: interactions.CommandContext, command: str):
#manage incoming commands
#this only works correctly when I use client = interactions.Client
#client.event
async def on_message(message):
#do things based on message contents
#this only works correctly when I use client = discord.Client
client.Start()
Thanks!
It would be on_message_create, as this is the name that the discord api uses

Discord.py commands wont run when used under #client.command()

i am attempting to use discord.ext commands to make my commands neater but the commands will not run
imports and intents:
import discord
from dotenv import load_dotenv
import requests
from discord.ext import commands
from discord.ext.commands import Bot
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
#Delcare imports
intents = discord.Intents().all()
client = commands.Bot(command_prefix='$', intents=intents)
command:
#client.command()
async def ping(ctx):
await ctx.send("pong")
i tried using
client = commands.Bot(command_prefix='$', intents=intents)
however the following error message occurred:
client = commands.Client(command_prefix='$', intents=intents)
AttributeError: module 'discord.ext.commands' has no attribute 'Client'
Try only using bot and not client:
import discord
from dotenv import load_dotenv
import requests
from discord.ext import commands
from discord.ext.commands import Bot
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents().all()
bot = commands.Bot(command_prefix='$', intents=intents)
#bot.command()
async def ping(ctx):
await ctx.send("pong")
bot.run('DISCORD_TOKEN')

discord.client: logging in using static token help me

enter image description here
Code:
import discord
import os
import sys
import asyncio
import time
import json
from discord.ext import commands
from discord.utils import get
bot = commands.Bot(command_prefix='*', intents=discord.Intents.all())
TOKEN= 'ODU1MDAwMzU3Njk2NDM4MzEy.Gxtt2S.IxUbC9bS8Ps3hTeUYpwxW_U7A6q0MuzTwJxUFA'
#bot.event
async def on_ready():
print(" Bot account ")
print()
print(f"Username: {bot.user.name}")
print(f"Bot ID: {bot.user.id}")
print(f"Bot token: {TOKEN}")
print()
bot.run(TOKEN)
Your code is throwing an discord.errors.PrivilegedIntentsRequired Exception as you can see in the bottom line of your screenshot.
This means it is trying to use intents that it has no permission to use. Either enable those intents in the discord developer portal or replace discord.Intents.all() with the intents that you really need and have sufficient permissions for.

discord.py commands not excecuting

I have multiple commands but none of thme seem to work, I had all of them in an on_message event, which I thought to be the prolem so I moved it all into seperate commands. There are no errors.
import discord
from googleapiclient.discovery import build
from discord.ext import commands
from PIL import Image, ImageFont, ImageDraw
import requests
import json
import textwrap
import random
yt_api_key = ''
youtube = build("youtube", 'v3', developerKey=yt_api_key)
description = '''blank'''
intents = discord.Intents.default()
client = discord.Client()
bot = commands.Bot(command_prefix='$', description=description, intents=intents)
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#bot.command()
async def test(ctx):
await ctx.send('test')
Thanks in advance!
I found the problem! I'm going to leave this here for anyone else with the same problem.
All I did was remove the bot variable and made it into the client one.
I think that the two were overlapping eachother.
import discord
from googleapiclient.discovery import build
from discord.ext import commands
from PIL import Image, ImageFont, ImageDraw
import requests
import json
import textwrap
import random
yt_api_key = ''
youtube = build("youtube", 'v3', developerKey=yt_api_key)
description = '''blank'''
intents = discord.Intents.default()
#-----------changed this---------------
client = commands.Bot(command_prefix='$')
#--------------------------------------
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#-------------and this----------------
#client.command()
#--------------------------------------
async def test(ctx):
await ctx.send('test')

I want Discord bot to write another python script output with comand

I did build a discord bot and i want to add a command that run & read another script output in python
how i can do that ?
here is my new bot on discord
import discord
from discord.ext import commands, tasks
import discord
from discord.ext import commands, tasks
from itertools import cycle
flashing = commands.Bot(command_prefix='.')
#flashing.command()
async def ping(ctx):
await ctx.send(f'Pong! {round(flashing.latency * 1000)} ms' )
print('bot is ready.')
#flashing.event
async def on_ready():
change_status.start()
flashing.run('token')
the other script name is base_script.py
assuming that the two scripts are in the same folder:
#scripts.py
def Some_Code():
#Put all of scripts.py code in this function
return(Variables)
#make sure to return all variable that you want to read in your other file
assuming you've set up scripts.py like that, the rest is easy:
import scripts
print(scripts.Some_Code())

Categories

Resources