I am using a script that I wrote in python for server muting a user via discord.py
I wrote it initially on my desktop and checked it worked (no issues), then put it in my python_scripts folder and ran it via terminal (no issues).
Finally, I then added a shell_command to run it from an automation and when I do, I get the following response:
stdout: None
stderr: |-
Traceback (most recent call last):
File "/config/python_scripts/discord_server_mute.py", line 11, in <module>
intents = discord.Intents.default()
^^^^^^^^^^^^^^^
AttributeError: module 'discord' has no attribute 'Intents'
returncode: 1
Conguration.yaml extract:
shell_command:
discord_mute: 'python3 /config/python_scripts/discord_server_mute.py {{ user_id }}'
Python script (/homeassistant/python_scripts/discord_server_mute.py):
import discord
from discord.ext import commands
import sys
# Replace these with your actual bot token and your user ID
TOKEN = 'REMOVED'
GUILD_ID = REMOVED
print(discord.__file__)
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='!', intents=intents)
async def mute_user(user_id):
guild = discord.utils.get(client.guilds, id=GUILD_ID)
if guild:
member = discord.utils.get(guild.members, id=user_id)
if member:
await member.edit(mute=True)
print(f'Muted {member.name}')
else:
print('User not found in the guild')
else:
print('Guild not found')
@client.event
async def on_ready():
user_id = int(sys.argv[1])
await mute_user(user_id)
await client.close()
client.run(TOKEN)
N.B. - discord.py is not advised for use by discord itself so do not use this code without knowledge of the topic please as it is your risk to take.
I have tried to double and triple check that there are no conflicts in the discord module but any help / ideas would be appreciated even if basic - I am not very experienced in python or home assistant console commands.
Thank you in advance.