Discord.py - Allow command in specific channel - python

i need to use my discord command in specific channel , I'm using #client.command()
I mean if i used my command in another channel will be not working
#client.command()
async def w(ctx):
channel = client.get_channel(775491463940669480)
await ctx.send("hello) ```

You can check if ctx.channel is equal to your specific channel.
#client.command()
async def w(ctx):
if ctx.channel.id == 775491463940669480:
await ctx.send("hello)

There is even an easier way to do it without getting the channel. And without having to indent the whole code.
#client.command()
async def w(ctx):
# exit command if not the desired channel.
if ctx.channel.id is not 775491463940669480:
return
#code here
await ctx.send("hello")

client = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print("bot is Online")
-
#client.event
async def on_message(message):
cmdChannel = client.get_channel(776179059355942943)
if message.content.lower().startswith('!w'):
if message.channel.id == cmdChannel.id:
#command invoked in command channel - execute it
await client.process_commands(message)
else:
#command attempted in non command channel - redirect user
await message.channel.send('Write this command in {}'.format(cmdChannel.mention))
#client.command()
async def w(ctx, amount):
await ctx.send(f"done {amount}")

async def is_channel(ctx):
return ctx.channel.id = 775491463940669480
#client.command()
#client.check(is_channel)
async def w(ctx):
await ctx.send("hello)
Making the check call the function is_channel makes the code simpler and you can resuse it multiple times.

Related

How to add spaces in async function discord.py

I am making a discord chat bot. I want to set command "How are You?" But my program only choose 'How' . I need some suggestions.TIA
import discord
from discord.ext import commands
client = commands.Bot(command_prefix= '=')
#client.event
async def on_ready():
print('Bot is online')
#client.command()
async def hello(ctx):
await ctx.send('hello')
#client.command()
async def how_are_you(ctx):
await ctx.send('I am good')
client.run('token')
If you want to use a command, i don't think you can register one with spaces in it, however there are a couple of workarounds:
First you can use the first word of your string as the command name and then check for the content of the message:
#client.command()
async def how(ctx):
if ctx.message.content.lower() == "=how are you":
await ctx.send("I'm fine thanks")
else:
pass
The = prefix is needed in the message, replace it when you want to change your prefix.
Or, the second option would be to set up an on_message listener.
#client.event
async def on_message(message):
if message.content.lower().startswith("=how are you"):
await message.channel.send("I'm fine thanks.")
await client.process_commands(message)

Why can't I create a command in Discord.py?

How to create a Discord.py command?
In fact, it doesn't show anything in the console at all, what should I do?
There's no error, nothing is executing.
import discord
import time
from discord.ext import commands
client = commands.Bot(command_prefix='/')
# This detects when the bot started.
#client.event
async def on_ready():
print(f'{client.user.name} is ready!')
# This detects when someone sends a message.
#client.event
async def on_message(message):
if message.author == client.user:
return
if 'fuck' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot swear in this discord server!')
if 'shit' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot swear in this discord server!')
if 'bitch' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot swear in this discord server!')
if 'https://' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot send links in this discord server!')
# This cteates a command.
#commands.command()
async def test(ctx, arg):
async with message.channel.typing():
time.sleep(1)
await ctx.send(arg)
client.add_command(test)
client.run('TOKEN')
You need to add client.process_commands at the end of the on_message event.
async def on_message(message):
# ...
await client.process_commands(message)
Another thing wrong in your code is that you're not defining message in the test command, you can fix it by using the Context.message attribute
async with ctx.message.typing():
# ...
# Or
async with ctx.typing():
# ...
Also you can simply use the client.command decorator instead of commands.command so you don't need to add the line client.add_command
#client.command()
async def test(ctx, arg):
# ...
Reference:
Bot.process_commands
Context.message
Context.typing

printing a bot.event() to a specific discord channel

I'm wanting to have my bot send it's "I connected to discord" message to a specific discord channel. Here is my code currently
import random
import discord
import time
from discord.ext import commands, tasks
bot = commands.Bot(command_prefix='!')
TOKEN = ''
#bot.event
async def on_ready(ctx):
channel = bot.get_channel(774970601013379092)
await channel.send(f'{bot.user.name} has connected to Discord!')
#bot.command()
async def hello(ctx):
await ctx.send(f"Hello World")
#bot.command()
#commands.is_owner()
async def shutdown(ctx):
await ctx.send(f"{bot.user.name} is shutting down. . .")
await ctx.bot.logout()
print(f'{bot.user.name} has disconnected from Discord!')
# c_sharp_challenges channel
#bot.command()
async def csharp(ctx):
channel = bot.get_channel(774683687018037249)
await channel.send('Welcome To C#!')
# html_challenges channel
#bot.command()
async def html(ctx):
channel = bot.get_channel(774683714399371316)
await channel.send('Welcome To HTML!')
# java_challenges channel
#bot.command()
async def java(ctx):
channel = bot.get_channel(774683618307342417)
await channel.send('Welcome To Java!')
# java_script_challenges channel
#bot.command()
async def javascript(ctx):
channel = bot.get_channel(774683653728501761)
await channel.send('Welcome to Java Script!')
# python_challenges channel
#bot.command()
async def python(ctx):
channel = bot.get_channel(774683556077109258)
await channel.send('Welcome To Python!')
bot.run(TOKEN)
I can't seem to find anything on stackoverflow, or google in general, that can help me figure this out. it's in python, and I'm on ubuntu 20.04 if that matters
That the bot sends an message to some channel that it is online. If you want to change the channel you just have to change the channel_id given in the on_ready function.
I happened to notice that I had 00000 in for the channel ID
#bot.event
async def on_ready():
print("bot is ready")
channel = bot.get_channel(774970601013379092)
await channel.send("bot is online")

#bot.command() AttributeError: 'Client' object has no attribute 'command'

from discord.ext import commands
client = discord.Client(command_prefix='x!')
#client.event
async def on_ready():
print("Successfully booted the bot up!")
#client.event
async def on_message(message):
if message.content.find("minecraft") != -1:
await message.channel.send('#Kerina#4436 ajde majnkraft jebemlite')
#client.command()
async def nwordpass(ctx):
await ctx.send('Proof of you having the nword pass: https://lh3.googleusercontent.com/1HBSAiHdRdM1UVJJZlOUnMkihkiMOPPYSMTjI5WzHuvDVIBztueZR83rkUiHwIJvrfU')
client.run("NzA2MzE0MTc3NjQzNDEzNTY1.Xq4cYw.A-6MruzAgtLC1maW4VVIB2HlFM4")
Why doesn't it work?
I tried almost every common fix but didn't get any positive results
You need to use the Bot class from discord.ext.commands
from discord.ext import commands
client = commands.Bot(command_prefix='x!')
#client.event
async def on_ready():
print("Successfully booted the bot up!")
#client.event
async def on_message(message):
if message.content.find("minecraft") != -1:
await message.channel.send('#Kerina#4436 ajde majnkraft jebemlite')
await client.process_commands(message)
#client.command()
async def nwordpass(ctx):
await ctx.send('Proof of you having the nword pass: https://lh3.googleusercontent.com/1HBSAiHdRdM1UVJJZlOUnMkihkiMOPPYSMTjI5WzHuvDVIBztueZR83rkUiHwIJvrfU')
I also added the await client.process_commands(message) line so that your commands are processed.
The sample code at https://discordpy.readthedocs.io/en/latest/ext/commands/extensions.html#id1 indicates that the correct code would look like this:
#commands.command()
async def nwordpass(ctx):
await ctx.send('Proof of you having the nword pass: https://lh3.googleusercontent.com/1HBSAiHdRdM1UVJJZlOUnMkihkiMOPPYSMTjI5WzHuvDVIBztueZR83rkUiHwIJvrfU')
That is, the command() decorator is defined on the commands module, not on the Client instance objects.

Discord.py auxiliary function not executing text

I have a running discord bot using Python, however, I am doing a rewrite to condense the code for other contributors to contribute code by adding an additional function that should reduce the reuse of code.
However, in the code, the function textCom is not able to recognize client.send_message
I have tried adding async to the def of the textCom function, however, this results in no response by the bot.
import discord
TOKEN = 'token'
client = discord.Client()
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
# commands cannot be executed in private messages
if message.channel.type == discord.ChannelType.private:
return
# original code
if message.content.startswith('!text1'):
msg = "text1 executed".format(message)
await client.send_message(message.channel, msg)
# new code I want executed
def textCom(textMessage):
msg = textMessage.format(message)
await client.send_message(message.channel, msg)
if message.content.startswith('!text2'):
textCom("text2 executed")
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run(TOKEN)
The python script run, however, when a user types !text1 the bot replies with "text1 executed" but when a user types !text2 the bot does not reply.
The interpreter should be raising a syntax error when you define on_message, before the bot even starts. You can't have an await inside a non-async function.
Make textCom an async def function and await it when you call it.
#client.event
async def on_message(message):
...
async def textCom(textMessage):
msg = textMessage.format(message)
await client.send_message(message.channel, msg)
if message.content.startswith('!text2'):
await textCom("text2 executed")

Categories

Resources