I am wanting to use MurkAPI through my Discord bot.
I want to somehow get https://murkapi.com/docs.php the python example working on my Discord bot so when someone types $fact in the server, it shows them a fact.
Below is a quick and dirty example of how to use aiohttp to access the endpoints and get data, then send it as a message.
from discord.ext import commands
bot = commands.Bot(command_prefix='$')
fact_url = "https://murkapi.com/fact.php?key={}".format(YOUR_API_KEY)
async def fetch_fact(session):
async with session.get(fact_url) as response:
return await response.text()
#bot.command(pass_context=True)
async def fact(ctx):
async with aiohttp.ClientSession() as session:
await bot.say(await fetch_fact(session))
bot.run("YOUR DISCORD TOKEN")
Related
Ok, so I have tried over 674 types of code, and none of them work. All I want is to make my bot reply to a message if a user says "?Pizza" and it replies "Wassuuuuuup"
Any help?
discord.ext.commands:
import discord
from discord.ext import commands
token = ""
bot = commands.Bot(command_prefix="?")
#bot.command()
async def pizza(ctx):
await ctx.send("Wassuuuuuup")
bot.run(token)
on_message():
#bot.event
async def on_message(msg):
if msg.author.id == bot.user.id:
return
if msg.content.startswith("?pizza"):
await msg.channel.send("Wassuuuuuup")
i am working on a discord.py bot and I want to run the discord.py client from the current event asyncio loop
from discord.ext import commands
import asyncio
async def start():
client = commands.Bot(command_prefix = '!')
#client.event
async def on_ready():
print("Ready")
client.run('token')
asyncio.run(start())
Is it possible to do such?
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'))
intents = discord.Intents.all()
client = discord.Client(intents=intents)
#bot.command()
async def hi(ctx):
await ctx.send("hi")
client.run(TOKEN)
I'm trying to get my bot to respond "hi" when a user runs command "!hi". The problem is that the command doesn't run and no errors show up as well. The code within the .py file is exactly as it is shown (except TOKEN is replaced with an actual Token)
You're running client, and decorating the command with bot.command(), you have to either use discord.Client or commands.Bot not both.
import discord
from discord.ext import commands
intents = discord.Intents.all()
bot = commmands.Bot(command_prefix=command.when_mentioned_or("!"), intents=intents)
#bot.command()
async def hi(ctx):
await ctx.send("Hi")
# You can also use events with `commands.Bot`
#bot.event
async def on_ready():
print("Ready")
bot.run("TOKEN")
The problem here is that you're using both discord.Client and commands.Bot
here's how to fix it:
import discord
from discord.ext import commands
TOKEN = "Your Token"
Intents=discord.Intents.all()
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), Intents=Intents)
#bot.command()
async def hi(ctx):
await ctx.send("hi")
client.run(TOKEN)
I am attempting to have a bot recognise when a user is typing and if that user is me, it sends a message. I don't know if I'm using this wrong but I read the API and at still doesn't make sense to me. the bot is not responding to any typing that I do in the default chat of my discord test server. I tried simplifying it to make sure that it actually wasn't seeing my typing and it still doesn't work, any ideas. the code is below.
import discord
from discord.ext import commands
token = ('~my token~')
client = commands.Bot(command_prefix = '.')
client.case_insensitive = (True)
#client.event
async def on_ready():
print('Ready')
#client.event
async def on_member_join(user):
print(f'~Temp~ {user} joined the server.')
#client.event
async def on_guild_join(server):
print(f'~Temp~ {server} joined')
#client.event
async def on_typing(channel, user, when):
print('Ready')
'''
if user.id == id1:
await channel.send('1')
elif user.id == id2:
await channel.send('2')
else:
await channel.send('3')
'''
#client.event
async def on_message(message):
print('~Temp~ Message found')
client.run(token)
You need to enable intents in order to work
intents = discord.Intents.default()
client = commands.Bot(.., intents=intents)
A Primer to Gateway Intents
I'm having issues with my code saying
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: edit() missing 1 required positional argument: 'self'
when I run the command. Self shouldn't need to be defined, right?
Also, when I do add self I get an issue with ctx.
The code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="/")
#client.command(pass_context=True)
async def join(ctx, member=discord.Member):
channel = ctx.author.voice.channel
await channel.connect()
await member.edit(mute=True)
#client.command(pass_context=True)
async def leave(ctx):
await ctx.voice_client.disconnect()
client.run("Token")
I Managed to find the problem and fixed it.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="/")
#client.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
await ctx.author.edit(mute=True)
#client.command()
async def leave(ctx):
await ctx.voice_client.disconnect()
client.run("Token")
the problem is, you included member on your funcion. If you want it to send, edit, or do something with the message author just do ctx.author and it will be set on the author of the message.